2

I have read the tutorials and prepared a list of checkboxes for the page. When the form is submitted, the Selected property only get the value false. Is there something I missed? The Model

public class SelectStudentModel
{           
    public int StudentID { get; set; }    
    public string CardID { get; set; }    
    public string Name { get; set; }    
    public bool Selected { get; set;}
}

The ViewModel

public class SelectStudentViewModel
{
    public List<SelectStudentModel> VMList;
    public SelectStudentViewModel()
    {
        VMList = SelectStudentModel.GETStudent();
    }
}

The View

@using Student.Models
@model SelectStudentViewModel
@using (Html.BeginForm("AddStudent", "SectionStudent", FormMethod.Post, new { @role = "form" }))
{
    @{ for (int i = 0; i < Model.VMList.Count(); i++)
    {
        <tr>
            <td>@Html.CheckBoxFor(m => m.VMList[i].Selected)</td>
            <td>@Html.DisplayFor(model => model.VMList[i].Name)</td>
        </tr>
    }
}
 <input type="submit" value="submit" />
}@* end form *@

The Controller for posted data

[HttpPost]
public ActionResult AddStudent(SelectStudentViewModel model)
{
    foreach (SelectStudentModel m in model.VMList)
    {
        Console.Write(m.Selected.ToString());
    }
    return PartialView("StudentSelectForm", model);
}
M. Ko
  • 563
  • 6
  • 31
  • You have appear to have shown us the wrong models The model in the view is `SelectStudentViewModel` but the only model you have shown is `SelectStudentModel` –  Mar 08 '16 at 05:34

1 Answers1

1

VMList is a field in your SelectStudentViewModel model. You need to change it to a property (with a getter/setter) so the DefaultModelBinder can set the values

public class SelectStudentViewModel
{
    public List<SelectStudentModel> VMList { get; set; } // change
    public SelectStudentViewModel()
    {
        VMList = SelectStudentModel.GETStudent();
    }
}

Side note: Suggest you change @Html.DisplayFor(model => model.VMList[i].Name) to @Html.LabelFor(m => m.VMList[i].Selected, Model.MList[i].Name) so that you get a label associated with the checkbox

  • That's it. Never known that could cause the problem. – M. Ko Mar 08 '16 at 05:47
  • You also have a lot of bad practice here. Perhaps you only showed a portion of the view, but the model in the view just needs to be `List` (you do not need another class to wrap it). And a view model should not contain objects or collections which are data models (what you should be using is a view model containing only those properties you need to in the view). And a view model should never call a method from a data model. You have made you app impossible to unit test (if you do need that view model as shown, then populate the collection from the controller) –  Mar 08 '16 at 05:51
  • Just started using ASP.NET MVC. Found that the pattern generates a lot of classes :P – M. Ko Mar 08 '16 at 06:58
  • Every view should have a corresponding view model, and once you start developing in MVC seriously you will discover that. It will save you a lot of time in the long run. See also [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Mar 08 '16 at 07:05
  • Would you recommend some sources where I can find nice and elegant tutorials for ASP.NET MVC? – M. Ko Mar 08 '16 at 07:10