First step:create a new MVC project
![2789632-acc6b3986e56908a.png](https://i-blog.csdnimg.cn/blog_migrate/979beba5e223e5a1b3166466ecb3bfdc.webp?x-image-process=image/format,png)
change the solution name and project name.
then choose project template:
![2789632-0db48b20e54cae1d.png](https://i-blog.csdnimg.cn/blog_migrate/b8d0169567166f843542c793aa8b9c94.webp?x-image-process=image/format,png)
![2789632-474fdae68a5a1a6d.png](https://i-blog.csdnimg.cn/blog_migrate/58081dddfe5ce11933a4cda8d967d2ca.webp?x-image-process=image/format,png)
Second step:add a controller and a view and a class.
![2789632-fe8cb45fc84a951d.png](https://i-blog.csdnimg.cn/blog_migrate/07f67ffb9af821e9ece8a11467b5d2de.webp?x-image-process=image/format,png)
Third step: edit the Index1.aspx in Views;
<div>
<form method="post" action="/Calculator/GetAverage">
请输入总分数:<input type="text" name="sumScore"/>
请输入总科目:<input type="text" name="sumObject">
<input type="submit" value="计算">
</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()
{
//接收数据
int sumScore = Convert.ToInt32(Request.Params["sumScore"]);
int sumObject = Convert.ToInt32(Request.Params["sumObject"]);
//调用models里的方法
GetAverage getAvg = new GetAverage();
int result = getAvg.GetAvg(sumScore,sumObject);
//保存需要传递的数据
ViewData["avgScore"] = "平均成绩为:" + result;
return View("Index1");
}
last step: in Views, get data from controler;
<%=ViewData["avgScore"] %>
ok, game over.
![2789632-9cbb3a7e5df06fd3.gif](https://i-blog.csdnimg.cn/blog_migrate/9bb7f20be421569e33a5e5c5e284454f.webp?x-image-process=image/format,png)