<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >
4. Use model
Controller
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >public actionresult Modeldemo () { user u= new User () {username= "li", password= "ABCDE"}; return View (U); } </span>
View<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><p> <%user u = (User) viewdata.model;%> UserName: <%= html.encode (u.username)%> </p> <p> Password: <%= html.encode (u.password)%> </p> </ Span>
Here are the different points of the four methods:ViewData is the Key/value dictionary collection, in the MVC1, there is the ViewData value is faster than ViewBag, ViewBag is a dynamic type object, starting from MVC3, is slower than viewdata, but good readability.
ViewData can only be used in the current action, while TempData is similar to a session and can be accessed across actions and is typically used to store error messages.
The model passes a strongly typed, so when you create a view, you create a strong view.
View to controller pass value
1. Read form data via Request.Form
View
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><% using (Html.BeginForm ("ActionName", "Controllername")) {%> username:<% html.textbox (" UserName "); %> password:<% html.textbox ("Password");%><%}%></span>
Controller<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >[acceptverbs (Httpverbs.post)] public actionresult actionname () { string username = request.form[" UserName "]; string password = request.form["password"]; return View ();} </span>
2. Read form data via FormCollection
View
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><% using (Html.BeginForm ("ActionName", "Controllername")) {%> username:<% html.textbox (" UserName "); %> password:<% html.textbox ("Password");%><%}%></span>
Controller
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >[acceptverbs (Httpverbs.post)] public actionresult actionname (formcollection formcollection) { String username = formcollection["username"]; string password = formcollection["password"]; return View (); } </span>
Summarize
Page Pass value will use a variety of methods, then the page and the controller between the value of the same can be a lot of methods. The value of view and controller is unavoidable, and mastering the method of transmission between them is helpful to develop more fluently.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
MVC Framework--view and controller pass-through values