-1

So I'm trying to pass an array of Ids to the controller of my ASP.NET application to save to the database, but every time I send the data, the arrays show up as empty when passed to the controller. Other data passes through okay but it's just the arrays.

Any help or recommendations would be greatly appreciated.

@using (Html.BeginForm("SaveCoursesFor", "Courses"))
{
    for (int i = 0; i < Model.ElectiveCount; i++)
    {
        <div class="form-group">
            @Html.Label("First Choice")
            @Html.DropDownListFor(c => c.FirstChoices[i], new SelectList(Model.AvailableCourses, "Id", "Name"), "Please select a course", new { @class = "form-control" })
            @Html.ValidationMessageFor(c => c.FirstChoices[i])
            @Html.Label("Second Choice")
            @Html.DropDownListFor(c => c.SecondChoices[i], new SelectList(Model.AvailableCourses, "Id", "Name"), "Please select a course", new { @class = "form-control" })
            @Html.ValidationMessageFor(c => c.SecondChoices[i])
        </div>
        <br />
    }    
    <button type="submit" class="btn btn-primary">Submit</button>
}

This is the model that's being passed:

public class SelectCoursesViewModel
{
    public List<Course> AvailableCourses { get; set; }
    public int[] FirstChoices;
    public int[] SecondChoices;
    public int ElectiveCount { get; set; }
    public SelectCoursesViewModel()
    {
        FirstChoices = new int[ElectiveCount];
        SecondChoices = new int[ElectiveCount];
    }
}

This is what shows up on the network tab:

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
Deathtraptaco
  • 21
  • 1
  • 9

1 Answers1

0

Try adding { get; set; } to your FirstChoices and SecondChoices. Those are needed for data binding to your model.

Personally, I'll also use List<int> for both fields as well so they're easier to work with.