This time is very busy, so the previous article did not reply to the message in time. Please forgive me.
Because it takes a long time, let's briefly review the content in highlights (2. Three problems are discussed: use of strong types in view and Mvc view user control, Use of UpdateModel and TryUpdateModel, and troubleshooting, in addition, the value of the tag element of the request page is accessed using FormCollection in the Action.
Using a strong type of view and Mvc view user control not only makes the page look comfortable but also readable. The following example is for your comparison.
If a strong type is used, you do not need to convert it on the page. Model is an Account object.
System. Web. Mvc. ViewPage <Account>
<% If (Model! = Null) {%>
<Div> <% = Model. Name %> </div>
<Div> <% = Model. Age %> </div>
<%} %
No strong type is used. ViewData storage object account is used here.
System. Web. Mvc. ViewPage
<% If (ViewData ["account"])! = Null {%>
<% Var account = ViewData ["account"] as Account; %>
<Div> <% = account. Name %> </div>
<Div> <% = account. Age %> </div>
<% }%>
UpdateModel and TryUpdateModel have some overload methods that can help us to accurately load data into objects, and nothing else.
Next I will introduce some practical skills of today to you, and proceed in the order of the previous article.
8. Use SelectList to fill the DropDownList
View:
<% = Html. DropDownList ("AccountType") %>
Controller:
List <AccountType> AccountType = GetAll ();
ViewData ["AccountType"] = new SelectList (accountType, "ID", "Name ");
You can add the default parameters:
ViewData ["AccountType"] = new SelectList (accountType, "ID", "Name", selectedValue );
9. Use Html. RenderPartial to call mvc usercontrol
<% Html. RenderPartial (ViewData ["PartialViewName"] as string, Model); %>
In development, the page is divided into several usercontrols, which not only makes the page logic clearer but also makes the defined usercontrol reusable.
10. ajax requests to the controller to return a set
In collection 1, we introduced an ajax request to the controller to delete records and return success or failure information. This time, we need to return a set of data. Because asp.net mvc integrates the jquery framework, therefore, use the getJson method to implement
Controller:
Public ActionResult GetAllAccount ()
{
List <Account> list = this. GetAllAccount ();
Return Json (list );
}
View:
$. GetJSON ('<% = Url. Action ("GetAllAccount", "Account") %> ',
Function (json ){
Var $ container = $ ("# container ");
$. Each (json, function (){
$ Container. append ('<div> <a href = 'detail? Id = '+ this. Id> this. Name </a> </div> ');
});
});
The code line return Json (list) in the controller transmits all the information to the client. It is best not to do this. Create a JsonAccount type with two attributes: ID, Name, you can only return the two information to the client. Of course, you can also use KeyValuePair, so that you do not need to define the JsonAccount type, which is more convenient to use!
Return Json (list. ConvertAll (a => new KeyValuePair <Guid, string> (a. ID, a. UserName )));
The client script must also change accordingly.
$ Container. append ('<div> <a href = 'detail? Id = '+ this. Key> this. Value </a> </div> ');
The practical collection of mvc is written here, hoping to help you develop the asp.net mvc program. Thank you for your support !!!!!