First step:create a new MVC project
change the solution name and project name.
then choose project template:
Second step:add a controller and a view and a class.
Third step: edit the Index1.aspx in Views;
<div>
<form method="post" action="/Calculator/GetAverage">
請(qǐng)輸入總分?jǐn)?shù):<input type="text" name="sumScore"/>
請(qǐng)輸入總科目:<input type="text" name="sumObject">
<input type="submit" value="計(jì)算">
</form>
</div>
fourth step: edit the models named GetAverage;
public class GetAverage
{
public int GetAvg(int sumScore, int sumObject)
{
return sumObject == 0 ? 0 : sumScore / sumObject;
}
}
fifth step: edit the controler ;
public ActionResult GetAverage()
{
//接收數(shù)據(jù)
int sumScore = Convert.ToInt32(Request.Params["sumScore"]);
int sumObject = Convert.ToInt32(Request.Params["sumObject"]);
//調(diào)用models里的方法
GetAverage getAvg = new GetAverage();
int result = getAvg.GetAvg(sumScore,sumObject);
//保存需要傳遞的數(shù)據(jù)
ViewData["avgScore"] = "平均成績(jī)?yōu)椋? + result;
return View("Index1");
}
last step: in Views, get data from controler;
<%=ViewData["avgScore"] %>
ok, game over.