Saturday, 15 February 2014

asp.net mvc - Put razor for-loop with indexed property into html partial -


Is it possible to put the Razor HTML view in partial view or to submit it with html.partial ("partial") or How can I, if possible, without breaking the sequencing of each field, which default model binder still can do its job correctly?

  @for (int i = 0; i & lt; model people .tv; i ++) {& lt; Tr & gt; @ Html.HiddenFor (m = & gt; m.People [i] .product ID) @HTMLEchideForM (M => M.Peepal [I] .name) @HTML.HededFor (M = & gt; M. people [i]. Description) & lt; Td> @if (hasMoreThanOnePeople) {@ Html.CheckBoxFor (m => m.People [i]. Iseelected)} and {@ Html.HiddenFor (m = & gt; m.People [i] is selected, new { Value = "true"}}} & lt; / Td> & Lt; TD & gt; @html DisplayFor (M => M. People [I] .name)  & Lt; TD & gt; @html DisplayFor (M = & gt; M. People [i]. Description) & lt; / Td> & Lt; / TR & gt; }  

right to use a custom EditorTemplate For your type of usage, assume that your class name is person , then

in the view / share / editor templates / person. CSST (Note that the name of the file should match the name of the class)

  @ Modal YourSaaxax Parson & lt; Tr & gt; & Lt; TD & gt; @ Html.HiddenFor (m = & gt; m.ProductId) @ Html.HiddenFor (m => m.Name) @ Html.HiddenFor (m => M.Description) @ html.CheckBoxFor (m = & Gt; m IsSelected) & lt; / Td> & Lt; TD & gt; @html DisplayFor (M => M. People [I] .name)  & Lt; TD & gt; @html DisplayFor (M = & gt; M. People [i]. Description) & lt; / Td> & Lt; TR & gt;  

then in the main view

  @model YourModel @using (HTML.BeginForm ()) {& lt; Table & gt; @ Html.EditorFor (m = & gt; m.People) & lt; / Table & gt; ...}  

To present the archive with the index EditorFor () The method is quite smart

Side notes:

  1. To create a specific EditorTemplate for a controller, you can see the file in view / YourControllerName / EditorTemplates / Person.cshtml
  2. one input one & lt; Tr & gt; element does not have a valid child & lt; Td> Insert the input inside the tag.
  3. Do not try to set the value attribute in an assistant. HTML assistants are designed to bind to your model. Instead, if your collection contains only one person object, set the value of its IsSelected property in the controller before passing the model See.

No comments:

Post a Comment