1

I'm using the package BeginCollectionItem for ASP MVC. I have a form to create and entity, which has an ICollection property. That property needs to be populated with items dinamically within the form.

This is how the 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 this is how the views look 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 I make a POST I see proper indexes assigned for each item in the 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 is that sometimes, randomly, some items of the Exercises collection that comes to my controller are null:

[ResponseType(typeof(Workout))]
public IHttpActionResult Post(WorkoutDTO workoutDto)
{
   var userId = User.Identity.GetUserId();
   ...
}

some null values

What can be causing this behaviour? I know that if I use a for loop with an integer index It can brings these problems because I can break the sequential binding, but these are GUIDs right?

Notes: My Model.Exercise_Create view model looks almost the same to the DTO.

UPDATE: Now I see that the only null items are the ones with a GUID index beginning with a letter. See the FormData I posted to 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 any custom data binding implemented, this is odd...

Hernan Demczuk
  • 395
  • 1
  • 3
  • 11

0 Answers0