MVC小練習(xí)——計算器(求平均數(shù))

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">
            請輸入總分數(shù):<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()
        {
            //接收數(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"] = "平均成績?yōu)椋? + result;
            return View("Index1");

        }

last step: in Views, get data from controler;

<%=ViewData["avgScore"] %>

ok, game over.

聯(lián)系方式

個人微信

公眾號_DotNet微說.jpg
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 快樂書法林昊元 七歲男童林昊元入惠文堂學(xué)習(xí)書法不到一年,最近又對畫畫產(chǎn)生興趣,開始教他玩色彩,涂涂、寫寫對于他來說...
    吳鴻發(fā)閱讀 642評論 2 1
  • 概念:獨立的損益表 損益表,簡單來說,就是:收入 - 成本 = 利潤。 在一個公司里,對成本承擔(dān)責(zé)任的部門,叫“成...
    洋_蔥頭閱讀 949評論 0 2
  • 從iOS開發(fā)轉(zhuǎn)至iOS測試開發(fā)已經(jīng)有四個月了,從最初連Appium是什么都不知道,到果斷舍棄,這是一邊學(xué)一邊實踐,...
    沈宥閱讀 540評論 1 7