目標:
完成一個簡單的整數加法
過程:
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函數的使用方法.