MVC小练习——计算器(求平均数)

First step:create a new MVC project

2789632-acc6b3986e56908a.png

change the solution name and project name.
then choose project template:

2789632-0db48b20e54cae1d.png
2789632-474fdae68a5a1a6d.png

Second step:add a controller and a view and a class.


2789632-fe8cb45fc84a951d.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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值