Read: 1543 comments: 15 Author: Shen Shan laolin posted on original article link
InASP. NET MVCInControllerAndViewTherefore, it is very important to master the data transmission methods between the two layers skillfully and flexibly. This article discusses from two aspects:
ØControllerDirectionViewTransmit data
ØViewDirectionControllerTransmit data
I,ControllerDirectionViewTransmit data
1.UseViewdataTransmit data
InControllerIs defined as follows:
Viewdata ["message"] = "Hello word !";
ThenViewReadingControllerDefined inViewdataData,CodeAs follows:
<% = Html. encode (viewdata ["message"]) %>
2.UseTempdataTransmit data
InControllerIs defined as follows:
Tempdata ["message"] = "Hello word !";
ThenViewReadingControllerDefined inTempdataThe Code is as follows:
<% = Html. encode (tempdata ["message"]) %>
3.UseModelTransmit data
UseModelWhen transferring dataViewWe will choose to create a strong typeViewAs shown in:
Create a strongly typedViewLater,ViewThe first line of code is as follows:
<% @ page title =" " language =" C # " masterpagefile = "~ /Views/shared/site. master " inherits =" system. web. MVC. viewpage " %>
<Mvcinduction. Models. People>It represents thisViewUsedModelForMvcinduction. Models. People"
Summary:
1. viewdata and tempdata the method is weak to transmit data, while the Model data transmission is strongly typed.
2. viewdata and tempdata are completely different data types. viewdata the data type is viewdatadictionary class instantiation object, the tempdata data type is tempdatadictionary class instantiation object.
3. tempdata actually saved in session , the controller starts from session obtain tempdata data and delete the session . tempdata data can only be transmitted once in the controller, and each element can only be accessed once, the access is automatically deleted.
4.ViewdataOnly oneActionTo read the relevant view page, only valid for the current view. Theoretically,TempdataYou canActionTo read multiple pages. However, in factTempdataThe element will be deleted after being accessed once.
II,ViewDirectionControllerTransmit data
InASP. NET MVC, SetViewThe data in is transmitted to the Controller, which is mainly implemented by sending forms. The specific methods are as follows:
1.PassRequest. FormRead form data
InViewThe layer is defined as follows:
<% Using (html. beginform ("actionname", "controllername "))
{%>
Username:<% Html. Textbox ("username"); %>
Password:<% Html. Textbox ("password"); %>
<% }%>
Note:ActionnameForActionName,ControllernameForControllerName
ThenControllerLayer, throughRequest. FormThe code for reading form data is as follows:
[Acceptverbs(Httpverbs. Post)]
Public ActionresultActionname ()
{
StringUsername = request. Form ["Username"];
StringPassword = request. Form ["Password"];
ReturnView ();
}
2.PassFormcollectionRead form data
InViewThe layer is defined as follows:
<% Using (html. beginform ("actionname", "controllername "))
{%>
Username:<% Html. Textbox ("username"); %>
Password:<% Html. Textbox ("password"); %>
<% }%>
ThenControllerLayer, throughFormcollectionThe code for reading form data is as follows:
[Acceptverbs (httpverbs. Post)]
Public actionresult actionname (formcollection)
{
String username = formcollection ["username"];
String Password = formcollection ["password"];
Return view ();
}
3.Custom Data Binding
To bind custom data, create a custom data binding class that inherits from imodelbinder to implement the bindmodel method in this interface.
The code is not listed due to a rush in writing. Sorry.
Conclusion: although we can useRequest. FormOrFormcollectionMethod To Read form data, but these two methods are generally cumbersome, in the strong typeViewWe usually useControllerBuilt-in methods of the base classUpdatemodel ()This method supports updating the attributes of an object using input form parameters. It uses the reflection mechanism to parse the attribute names of an object, and then automatically assigns values to relevant attributes based on the parameter values passed by the client.
The following isDemoUseUpdatemodelExample code:
Sample Code using updatemodel () [Acceptverbs (httpverbs. Post)]
Public Actionresult edit ( Int ID, formcollection collection)
{
// Users user = userrepository. getuser (ID );
// User. Username = request. Form ["username"];
// User. Password = request. Form ["password"];
// User. Telephone = request. Form ["telephone"];
// User. Address = request. Form ["Address"];
// The above method is a bit cumbersome, especially after the exception handling logic is added. A better way is to use the built-in method updatemodel () of the controller base class (). This method supports updating the attributes of an object using input form parameters. It uses the reflection mechanism to parse the attribute names of an object, and then automatically assigns values to relevant attributes based on the parameter values passed by the client.
Users user = Userrepository. getuser (ID );
String [] Allowedproperties = New [] { " Username " , " Password " , " Telephone " , " Address " };
Updatemodel (user, allowedproperties );
Userrepository. Save ();
ReturnRedirecttoaction ("Details",New{ID=User. ID });
}
Post comments
News Channel: 9 city EA joint launch of online games "FIFA online"
Recommended link: Windows 7 topic release
Website navigation: blog homepage personal homepage news community blog flash memory Knowledge Base