Apologies if this has been asked before; there are a million ways to phrase it so searching for an answer has proved difficult.
I have a viewmodel with the following properties:
public class AssignSoftwareLicenseViewModel
{
public int LicenseId { get; set; }
public ICollection<SelectableDeviceViewModel> Devices { get; set; }
}
A simplified version of SelectableDeviceViewModel would be this:
public class SelectableDeviceViewModel
{
public int DeviceInstanceId { get; set; }
public bool IsSelected { get; set; }
public string Name { get; set; }
}
In my View, I am attempting to display a list of editable checkboxes for the Devices property, inside an input form. Currently, my View looks like this:
@using (Html.BeginForm())
{
@Html.HiddenFor(x => Model.LicenseId)
<table>
<tr>
<th>Name</th>
<th></th>
</tr>
@foreach (SelectableDeviceViewModel device in Model.Devices)
{
@Html.HiddenFor(x => device.DeviceInstanceId)
<tr>
<td>@Html.CheckBoxFor(x => device.IsSelected)</td>
<td>@device.Name</td>
</tr>
}
</table>
<input type="submit" value="Assign" />
}
The problem is, when the model gets posted back to the controller, Devices is null.
My assumption is that this is happening because even though I'm editing its contents, the Devices property is never explicitly included in the form. I tried including it with HiddenFor, but that just resulted in the model having an empty list instead of null.
Any idea what I'm doing wrong here?
What ViewModel is. In ASP.NET MVC, ViewModels are used to shape multiple entities from one or more models into a single object. This conversion into single object provides us better optimization. You can see the concept of ViewModel in the image below.
These are the complex scenarios in which ViewModel can be used for better maintainability, reliability, and testability of code. Let’s take an example in which there is a View for showing a list of books. And, we also want to show the list of customers who rented these books.
You can see the concept of ViewModel in the image below. As you can see, if we want to display more than one Model into a single View, we have to pass a ViewModel to that View, so that we can take benefits of both the models into a single object.
Let’s create a project with the name ViewModelsDemo. Then, select Empty, check MVC, then click OK. Now, let’s create the model classes for both - Book and Customer. Give that class the name “Book” and click OK. And then, write the following code into it.
My assumption is that this is happening because even though I'm editing its contents, the Devices property is never explicitly included in the form.
No, your assumption is wrong. The reason this doesn't get bound properly is because your input fields doesn't have correct names. For example they are called name="IsSelected"
instead of name="Devices[0].IsSelected"
. Take a look at the correct wire format that needs to be used to bind to collections: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
But why this happens?
It happens because of the foreach
loop that you used in your view. You used x => device.IsSelected
as lambda expression for the checkbox but this doesn't take into account the Devices property at all (as you can see by looking at the generated source code of your web page).
So what should I do?
Personally I would recommend you using editor templates as they respect the navigational context of complex properties and generate correct input names. So get rid of the entire foreach
loop in your view and replace it with a single line of code:
@Html.EditorFor(x => x.Devices)
and now define a custom editor template that will automatically be rendered by ASP.NET MVC for each element of the Devices collection. Warning: the location and name of this template are very important as this works by convention: ~/Views/Shared/EditorTemplates/SelectableDeviceViewModel.cshtml
:
@model SelectableDeviceViewModel
@Html.HiddenFor(x => x.DeviceInstanceId)
<tr>
<td>@Html.CheckBoxFor(x => x.IsSelected)</td>
<td>@Html.DisplayFor(x => x.Name)</td>
</tr>
Another approach (which I don't recommend) is to change your current ICollection
in your view model to an indexed collection (such as an IList<T>
or an array T[]
):
public class AssignSoftwareLicenseViewModel
{
public int LicenseId { get; set; }
public IList<SelectableDeviceViewModel> Devices { get; set; }
}
and then instead of the foreach use a for
loop:
@for (var i = 0; i < Model.Devices.Count; i++)
{
@Html.HiddenFor(x => x.Devices[i].DeviceInstanceId)
<tr>
<td>@Html.CheckBoxFor(x => x.Devices[i].IsSelected)</td>
<td>@Html.DisplayFor(x => x.Devices[i].Name</td>
</tr>
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With