美文网首页.Net微说集C#dotNET
MVC小练习——计算器(求平均数)

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

作者: 张中华 | 来源:发表于2017-07-01 15:54 被阅读126次

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.

相关文章

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

    First step:create a new MVC project change the solution ...

  • pandas 基础入门: 向量化方法

    求某列数据的平均数(mean) 此处需要用到numpy求test列的平均数: numpy.mean(test)求t...

  • iOS简单项目 — 计算器

    计算器所需求功能 计算器所用模式:MVC计算器所用语言:Swift计算器所参考课程:斯坦福 第一步:构造计算器界面...

  • 前端(十三)

    1.小作业 2.if练习2 3.if练习3 4.switch练习1 5.switch练习2 6.计算器

  • 求平均数

    题目:求给定一组数的平均数,要求去掉一个最大值,去掉一个最小值,再求平均值。 类数组转化为数组:把argument...

  • 赚大钱

    老师看到小尚同学经常不爱学习,老爱练习操作电子计算器:别人专心听讲的时候,他在练习操作电子计算器;别人仔细做题的时...

  • 10.13

    题目一: 输出 题目二: 输出小 作业三:求正方形的面积 作业四: 求正方形的面积( 2 ) 作业五: 求三个数的平均数

  • MySql(六)分组函数

    AVG () 求平均数 SUM () 求和 COUNT () 计数 MAX () 求最大值 MIN () 最小值 ...

  • 平均数教学案例 2022-03-06

    平均数 教学内容:“平均数”北师大版小学数学四年级(下)第六单元 教学目标:1.经历探究求平均数的过程,体会平均数...

  • 10.13

    输出 输出小数 求正方形面积 求正方形面积2 求三个数的平均数

网友评论

  • 高调的小丑:哈哈,这种小练习的方式还真是不错:+1:
    张中华:@高调的小丑 哈哈,一个知识点一个知识点解决吗,毕竟自己是个小菜鸟,差的东西还很多……

本文标题:MVC小练习——计算器(求平均数)

本文链接:https://www.haomeiwen.com/subject/bagwcxtx.html