Model display settings in MVC4 (with display shared/displaytemplates and edit shared/editortemplates)

Source: Internet
Author: User

Although [display (name= "XXX")] has been able to display its values in the page @html.labelfor (m=m. Properties), it is not flexible enough, especially at @Html.etitorformodel () or @ Html.displayformodel (), we want to display the information according to our own requirements, then we need to

The implementation steps are as follows:

1. Add the editortemplates folder under the shared folder under View

2. Add views in editortemplates (do not inherit any templates) such as Add View yesorno.cshtml

3. Add processing code to yesorno.cshtml such as: The user fills in is married, at this time we want in that bool type value in the page to @html.radiobutton () the way

To let the user choose this time we can write the following code in yesorno.cshtml

BOOL @Html. RadioButton ("",true, Model) is @html.radiobutton ("",false ,! Model) No

4. In model, use the Setview in UIHint("Setview") to display the current property in the model, as shown in the code below

        [UIHint("yseorno")]          Public BOOL Get set; }

5. In the edit page we just use @html.etitorformodel () in the form to display it.

@{    "Index";     " ~/views/shared/_layout.cshtml " ;} @model mvcapplication1.models.user@using (Html.BeginForm ()) {     @Html. Editorformodel ()    @Html. Editorfor (M == M.list    )<inputtype= "submit" value= " Submit " />}

Note that one of the above for a number of friends when the situation also through the above way UIHint to set the display, because it is a one-to-many relationship, so can not be displayed, then we have to manually add

Add @html.editorfor (m=>m.list), Setview (that is, myfriend.cshtml in the EditorTemplates folder) code as follows

@model ienumerable<mvcapplication1.models.friend><div>     @foreach (varin  Model)     {          <div>                  @Html. labelfor (M=>item. Name): @Html. Textboxfor (m=>item. Name)                  @Html. Labelfor (M=>item. Age): @Html. Textboxfor (m=>item. Age)         </div>     }</div>

corresponding relationship in Mode

 [UIHint ( myfriend   " )]  public  list<friend> List {get ; set      public  class   Friend { public  string  Name { get ; set          public  int  Age {get ; set     

The use of the corresponding enumeration or class in the above process model

        [UIHint ("myrole")]          Public Get Set ; }    }      Public enum Role    {         Admin,        pm, one        ,    }

Add code in myrole.cshtml in the editortemplates folder

@model MvcApplication1.Models.Role<SelectId="role"Name="role">@foreach (MvcApplication1.Models.Role iteminchEnum.getvalues (typeof(MvcApplication1.Models.Role))) {<option value="@item"@ (Model==item?)"selected=true":"") > @item </option>    }</Select>

Classes do the same, or you can use ViewData to make a fuss

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.