The MVC4 of the AspNet teaching -2:aspnet MVC4 Random questions Add a continuation of the last MVC course

Source: Internet
Author: User

On the basis of the Mvcaddtest project of the previous course, we continue to improve the deduction, add the random question, and judge the function of the correctness.

On the original basis, add a class file, the code is as follows:

Using system;using system.collections.generic;using system.linq;using system.web;namespace MvcAddTest.Models{Public        Class Randnum {private int firstnum;             private int secondnum;            Public Randnum (bool BR) {if (BR = true) return;            Random r1=new random (); Firstnum = R1.            Next (100);            Random r2 = new Random (); Secondnum = R2.                         Next (50);            } public int Firstnum {get {return firstnum;                } set {firstnum = value;            Return            }} public int Secondnum {get {return secondnum;                } set {secondnum = value;            Return }        }                  }}

To modify the original addcal.cshtml file:

@model mvcaddtest.models.randnum@{viewbag.title = "addcal"; } 
The HomeCtroller.cs code is modified as follows:
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;using         Mvcaddtest.models;namespace mvcaddtest.controllers{public class Homecontroller:controller {//GET:/Home/        Public ActionResult Index () {return View ();            } [HttpGet] public ActionResult addcal () {randnum rnobj = new Randnum (true);            Viewdata.model = Rnobj;            @ViewBag. strresult = "";            Viewbag.strokerr = "";        return View ();            } [HttpPost] public actionresult addcal (string firstnum, String secondnum,string sumnum) {                       int A, b, C; a= Int.            Parse (Firstnum); b= Int.                        Parse (Secondnum); c = Int.           Parse (Sumnum);           Randnum rnobj = new Randnum (false);           Rnobj.firstnum=a;            Rnobj.secondnum=b;         if (c! = (A + b)) Viewbag.strokerr = "ERR";  else Viewbag.strokerr = "OK";            Viewbag.strresult = C.tostring ();            Viewdata.model = Rnobj;        return View (); }    }}


The MVC4 of the AspNet teaching -2:aspnet MVC4 Random questions Add a continuation of the last MVC course

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.