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

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">
            请输入总分数:<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.

联系方式

个人微信

公众号_DotNet微说.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 快乐书法林昊元 七岁男童林昊元入惠文堂学习书法不到一年,最近又对画画产生兴趣,开始教他玩色彩,涂涂、写写对于他来说...
    吴鸿发阅读 3,828评论 2 1
  • 概念:独立的损益表 损益表,简单来说,就是:收入 - 成本 = 利润。 在一个公司里,对成本承担责任的部门,叫“成...
    洋_葱头阅读 4,472评论 0 2
  • 从iOS开发转至iOS测试开发已经有四个月了,从最初连Appium是什么都不知道,到果断舍弃,这是一边学一边实践,...
    沈宥阅读 3,493评论 1 7

友情链接更多精彩内容