[2017-AspNet-MVC4] 案例演化:加法測試-1

目標:

完成一個簡單的整數加法

過程:

1.新建一個Basic類型的MVC4的Project

2.新建一個Home Controller,代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AddTest.Controllers
{
    public class HomeController : Controller
    {
   
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult CalSum(string FirstNum,string SecondNum)
        {
            int a, b, c;
            a = int.Parse(FirstNum);
            b = int.Parse(SecondNum);
            c = a + b;
            ViewBag.vc = c.ToString();
            return View("Index");
        }

    }
}
3.爲Index Action新建一個相應的View,代碼如下:

@{
    ViewBag.Title = "AddTest";
}

<h2>AddTest</h2>
@using (Html.BeginForm("CalSum", "Home"))
{
   
    @Html.TextBox("FirstNum") <b>+</b>
    @Html.TextBox("SecondNum") <b>=</b>
    @Html.TextBox("SumNum",(string)ViewBag.vc)
    <input type="submit"/>
}

小結:

掌握ViewBag對象以及int.Parse函數的使用方法.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章