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