1

I cannot seem to access a multiple select list's values when posting.

The code in the view is:

<select id="instructors" name ="instructors" multiple="multiple">
    @{
        List<CMMS.WebUI.ViewModels.AssignedInstructor> instructors = ViewBag.Instructors;

        foreach (var instructor in instructors)
        {
            if (instructor.HasInstructor)
            {
                @:<option value="@instructor.InstructorId" selected="selected">@instructor.InstructorName</option>
            }
            else
            {
                @:<option value="@instructor.InstructorId">@instructor.InstructorName</option>
            }
    }
}
</select>

I have tried grabbing the values in the controller via the following ways:

[HttpPost]
public ActionResult Edit(int id, FormCollection formCollection)
{
    string[] selectedInstructors = formCollection["instructors"];
    ...
}


[HttpPost]
public ActionResult Edit(int id, string[] instructors)
{  
    ...
}

Both ways the instructors are always null. If anyone can tell me where I am screwing this up, I would appreciate it very much.

4

1 に答える 1

0

代わりに for ループを使用してみてください

for (int i = 0; i < instructors.Count(); i++)
{
    if (instructor[i].HasInstructor)
    {
     ...
于 2012-10-24T20:29:22.880 に答える