When my model is not valid, I want to return the view with the check checkbox.
How do you change your code? Is it possible with my attitude?
View
@ Model ListTest.Models. People ListWellModel @ {var HammerthonOnepapers = @Models People. Count & gt; 1; } @html.business ("save", "home") {@ HTML.ValidationSummary (wrong) & lt; Table & gt; @wunch (model in the model. People) {& lt; Tr & gt; @ If (Hammorethanonpersons) {& lt; Td> & Lt; Input type = "checkbox" name = "selected idioms" value = "@ item.piser id" /> & Lt; / TD & gt; } Other {@ Html.Hidden ("SelectedIds", item.PersonId)} & lt; Td> & Lt; Input type = "text" value = "@ item.Name" /> & Lt; / TD & gt; & Lt; / TR & gt; } & Lt; / Table & gt; & Lt; Input type = "submit" value = "save" /> }
VIEWMODEL
public class PeopleListViewModel {public PeopleListViewModel () {SelectedIds = new int [] {}; } [Minenamban (1, error message = "Minimum one person should be selected!")] Public Ent [] Selected EDS {Received; Set; } Public listing & lt; Person & gt; People {receive; Set; }}
Administrators
Public Action Index () {var people = new list & lt; Person & gt; {New person {name = "horst", person income = 10}, new person {name = "Michael", person income = 20}}; See refresh (new locallistview model {people = people}); } [HTPOPist] Public Action Result Save (View Peoples List View Model) {if (ModelState. ISI Walid) {} VisionModel. People = new list & lt; Person & gt; {New person {name = "horst", PersonID = 10}, new person {name = "Bernard", PersoniaD = 20}}; See Return ("Index", View MODEL); }
Some things to change
First of all, To include a IsSelected
property to the people
model, we have your SelectedIds
method
Want to finish with the other, so that the data from the client to post, we will have to rewrite your foreach
to
so that the field is correctly To be indexed, we will hide some additional s If you do not want (because when your validation fails, then we are not Populing again your model), your table will be the following:
& lt; Table & gt; For @ (Int I = 0; I & lt; Model.log.accounts; i ++) {& lt; Tr & gt; @ Html.HiddenFor (m = & gt; m.People [i]. PersonID) @ Html.HiddenFor (m => m.People [i] .name) @ if (Hammorthononpersons) {& lt; Td> @html Checkboxfor (M = & gt; M.Peepol [I]. Season selected) & lt; / Td> } And {@ Html.HiddenFor (m = & gt; m.People [i] .Is selected)} & lt; Td> & Lt; Input type = "text" value = "@model. People [i] .name" /> & Lt; / TD & gt; & Lt; / TR & gt; } & Lt; / Table & gt; Finally, if your verification fails, we do not reassign your
people
list in your Action Method. Use the code I select to add the following people. Also, because we do not have the selected IDEs
and we can verify our own:
[HTTP post] Public Action Result Service (see Loklist View ModelModel } {List & lt; People & gt; Selected = ViewModel. People Where (P => PIS selected). Ollist (); If (selected. Any ()) {// is a valid list & lt; Int & gt; SelectIds = Selected. Select (s = & gt; s PersonID) .toList (); } View return ("index", see modell); }
No comments:
Post a Comment