Record learning MVC process, controller methods and Views (i)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.