c# - Not all values in MVC form being posted -
a little or advice if of can? have mvc form (it's umbraco form/surface controller, don't know that has bearing on this), model contains list of objects being used generate check-boxes using html helper methods; see following post how rendering these , receiving them in child action: https://stackoverflow.com/a/20687405/1285676
all appears working well, except although check-boxes being posted on submit, not being picked model received actionresult method in controller. have looked @ posted data using network tools in chrome, , appears ok.. (all check-boxes appear being posted in same format), million dollar question is, preventing values being picked in model?
here's snipet of form rendering check-boxes, cut down rest should not relevant (yes there submit button in real thing..):
@model memberpreferencesvm @using (html.beginumbracoform<memberaccountsurfacecontroller>("handledealermemberpreferencesaccountupdate", formmethod.post, new { @class = "", @id = "dealer-member-account-preferences-form" })) { @html.antiforgerytoken() @html.validationsummary(true) @for (var j = 0; j < model.brands.count; j++) { if (model.brands[j].division == model.divisions[i].id.tostring()) { var divisionselected = model.divisions.any(x => x.id.tostring() == model.brands[j].division) ? model.divisions.firstordefault(x => x.id.tostring() == model.brands[j].division).checked : false; var checkboxattr = new dictionary<string, object> { { "class", "styled" }, { "data-division", model.brands[j].division } }; var brandselected = model.brands[j].checked; <div class="col-xs-4"> <label class="option @if (divisionselected && brandselected){<text>active</text>}"> <img src="@(model.brands[j].logourl)" class="center-block" alt="@(model.brands[j].name)" /> <span class="checkbox"> @html.hiddenfor(model => model.brands[j].id) @html.hiddenfor(model => model.brands[j].name) @html.checkboxfor(model => model.brands[j].checked, checkboxattr) </span> </label> </div> } } }
slightly cut down version of model:
public class memberpreferencesvm: ivalidatableobject { public memberpreferencesvm() { init(); } private void init() { divisions = [logic fetch divisions]; brands = [logic fetch brands]; } public ilist<divisionvm> divisions { get; set; } public ilist<brandvm> brands { get; set; } public ienumerable<validationresult> validate(validationcontext validationcontext) { var pdivisions = new[] { "divisions" }; var pbrands = new[] { "brands" }; if (!divisions.any(x => x.checked)) { yield return new validationresult("select @ least 1 division view default", pdivisions); } if (!brands.any(x => x.checked)) { yield return new validationresult("select @ least 1 brand view default", pbrands); } } }
brandvm:
public class brandvm { public int id { get; set; } public string name { get; set; } public string logourl { get; set; } public bool checked { get; set; } public string division { get; set; } }
the actionresult:
[validateantiforgerytoken] [validateinput(true)] [httppost] public actionresult handledealermemberpreferencesaccountupdate(memberpreferencesvm model) { //logic removed model doesn't arrive values not relevant return currentumbracopage(); }
the model should contain 24 brands, ever seems arrive @ 'handledealermemberpreferencesaccountupdate' 12.
any appreciated (am hoping it's simple i've slipped up)..
mark
ok, turned out issue data, shouldn't have been possible.
stephen muecke's comment above indeed correct
you have if block inside loop. if ever evaluates false means skip 'indexer' , binding fail when submit. default, collection indexers must start @ 0 , consecutive. while can add input indexer make work, should using view model contains objects need in view.
essentially, 1 of brands didn't have division associated , broke solution not being posted controller correctly. code need refactoring isn't possible, though have made changes in cms prevent happening again.
hopefully else, pointers.
Comments
Post a Comment