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);
}
1
  • 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 Commented Mar 8, 2016 at 5:34

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

That's it. Never known that could cause the problem.
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<SelectStudentModel> (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)
Just started using ASP.NET MVC. Found that the pattern generates a lot of classes :P
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?
Would you recommend some sources where I can find nice and elegant tutorials for ASP.NET MVC?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.