c# - Why do I get random null items in a collection after model binding? -
i'm using package begincollectionitem asp mvc. have form create , entity, has icollection property. property needs populated items dinamically within form.
this how dto looks:
public class workoutdto { public int id { get; set; } //some other properties public icollection <exercise> exercises { get; set; } } public class exercise { public int exerciseselected { get; set; } //some other properties } and how views like:
@using (html.beginform("create", "project", formmethod.post)) { //bunch of other inputs @html.partial("_exercise_form", model.exercise_create) <input class="btn btn-action add-exercise-button" type="button" value="add"> } exercise_form partial view:
@model trackme.web.viewmodels.workout_create @using (html.begincollectionitem("exercises")) { @html.hiddenfor(m => m.id) @html.dropdownlistfor(m => m.exerciseselected, new selectlist(model.exercises, "id", "name"), "exercise", new { @class = "form-control" }) @html.textboxfor(m => m.repetition1, new { @class = "form-control", placeholder = "rep" }) @html.textboxfor(m => m.weight1, new { @class = "form-control", placeholder = "kg" }) @html.textboxfor(m => m.repetition2, new { @class = "form-control", placeholder = "rep" }) @html.textboxfor(m => m.weight2, new { @class = "form-control", placeholder = "kg" }) @html.textboxfor(m => m.repetition3, new { @class = "form-control", placeholder = "rep" }) @html.textboxfor(m => m.weight3, new { @class = "form-control", placeholder = "kg" }) @html.textboxfor(m => m.repetition4, new { @class = "form-control", placeholder = "rep" }) @html.textboxfor(m => m.weight4, new { @class = "form-control", placeholder = "kg" }) @html.textboxfor(m => m.repetition5, new { @class = "form-control", placeholder = "rep" }) @html.textboxfor(m => m.weight5, new { @class = "form-control", placeholder = "kg" }) } everything works fine, when make post see proper indexes assigned each item in collection:
exercises.index:ae5247e5-b207-488d-84f9-3e2a9a8423ff exercises[ae5247e5-b207-488d-84f9-3e2a9a8423ff].id:0 exercises[ae5247e5-b207-488d-84f9-3e2a9a8423ff].exerciseselected:3 exercises.index:352598b5-9264-4c8d-9501-ac5deb6e911d exercises[352598b5-9264-4c8d-9501-ac5deb6e911d].id:0 exercises[352598b5-9264-4c8d-9501-ac5deb6e911d].exerciseselected:3 exercises.index:b64be97b-adbf-4d03-98d0-9d4f13d84ac6 exercises[b64be97b-adbf-4d03-98d0-9d4f13d84ac6].id:0 exercises[b64be97b-adbf-4d03-98d0-9d4f13d84ac6].exerciseselected:6 the problem sometimes, randomly, items of exercises collection comes controller null:
[responsetype(typeof(workout))] public ihttpactionresult post(workoutdto workoutdto) { var userid = user.identity.getuserid(); ... } 
what can causing behaviour? know if use loop integer index can brings these problems because can break sequential binding, these guids right?
notes: model.exercise_create view model looks same dto.
update: see only null items ones guid index beginning letter. see formdata posted confirm this:
1) exercises.index:ae5247e5-b207-488d-84f9-3e2a9a8423ff --> null 2) exercises.index:352598b5-9264-4c8d-9501-ac5deb6e911d --> not null 3) exercises.index:b64be97b-adbf-4d03-98d0-9d4f13d84ac6 --> null i dont have custom data binding implemented, odd...
Comments
Post a Comment