1//a simple registration class (without any processing, followed by additional content)
public class UserInfo {public virtual int Id {get; set;} Public virtual string Name {get; set;} public virtual int Age {get; set;} }
The biggest difference between 2.MVC and aspx is that MVC returns a method in which the address bar is actually the method name, not the page
#region user list public actionresult Index () { list<userinfo> user = new list<userinfo> (); for (int i = 0; i <; i++) { user. ADD (New userinfo{id=i,name= "s" +i}); Viewbag.dt = user; return View (user); #endregion
A controller corresponding to a view page, click on the controller's method can be returned to the view, or directly add the view
This is the WebForm and MVC of the base and MVC, the biggest difference is the call method, one is to use @ one is <%:xxx%>
Dynanic keyword, built-in index, better than Var, because the return is an entity, you can use the forced class conversion
<% foreach (Dynamic T in Model)
{%> <%:t.Name%> <%:t.id%><br/> <%} %>
3. Simple user registration (not making judgments and using HTML paving method, follow up learning), form is presented to the Controller method, similar to ASHX
<form method= "POST" action= "/user/processadd" > <table> <tr> <td> user name </td > <td><input type= "text" name= "UserName"/></td> </tr> <tr> < Td> Age </td> <td><input type= "text" name= "Userage"/></td> </tr> < tr> <td colspan= "2" ><input type= "submit" value= "register new user"/></td> </tr> </table> </form>
The controller corresponding method receives the returned value
Public ActionResult PROCESSADD (formcollection collection) { //formcollection table only son comes in a collection equivalent to request["UserName" ] String userName = request["UserName"]; int age; BOOL B = Int. TryParse (collection["age"]?? " 0 ", out of age); Return Content ("OK"); Return redirecttoaction ("Index"); }
4. Display user Information
#region Show user Details public actionresult Show () { UserInfo user = new UserInfo (); User. Id = 1; User. Name = "Xiao Li"; User. Age = +; Viewbag.userinfo = user; Viewdata.model = user;//value Model to Viewdata.model force type View return view (); } #endregion
Front page. Because it's a model,model. is a mandatory type view
Id:<%:model.id%> name:<%:model.name%> <%:html.textboxfor (u=>u.name)%>// For use is the LAMDA expression, meaning to define a U (meaning he is lamda expression)goes to his attributes (-_-Baidu Brain Tonic)
<%:html.textbox ("name")%>//the value corresponding to name will automatically match
Record learning MVC process, controller methods and Views (i)