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