When my index displays 2 people and none of these are selected.
Now comes my custom logic in the UI. When only one person is displayed in the index view, then I do not want a checkbox to check it. Customers can press the submit button directly. I try to fill the selectedIds array manually by viewing the @else section.
But this code: Model.SelectedIds = new int [] {item.PersonId};
does not work, View MODEL. Selected property on server side action is always {int [0]}
How can I still specify an ID selected for an individual ID? Array?
VIEW
@ Model ListTest.Models. People ListViewModel @ {var HammerthononPerson = @model.log.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 ids" value = "@ item.psirid" /> & Lt; / TD & gt; } Other {models. Selected IDS = new entry [] {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 = "hoirst", PersonID = 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); }
You may consider submitting a hidden field in this case so that This value will be sent back to your server for sure:
@if (hasMoreThanOnePerson) {& lt; Td> & Lt; Input type = "checkbox" name = "selected idioms" value = "@ item.piser id" /> & Lt; / TD & gt; } Else {@ Html.Hidden ("SelectedIds", item.PersonId)}
But there is clearly a better way to handle it on the server - which means that if any Do not assume in the visual model you will only bring it back from the backend because the user did not have the chance to modify this value in the UI because it did not have any input element that it could manipulate.
No comments:
Post a Comment