I hope that I don’t miss something incredibly obvious, but is there a reason why the connecting device always has difficulty linking a view model that inherits from the collection?
Suppose I want to show a paged list and display a combo box and add a button above it (dealing with simple lists). The classes you enter will look like this:
public class PagedList<T> : List<T> { public int TotalCount { get; set; } }
And then a view model that looks like this:
public class MyViewModel : PagedList<ConcreteModel> { public IEnumerable<ChildModel> List { get; set; } public int? SelectedChildModelId { get; set; } }
So in the view (Razor):
@model MyViewModel @using (Html.BeginForm()) { @Html.DropDownListFor(model => model.SelectedChildModelId, new SelectList(Model.List, "ChildModelId", "DisplayName")) }
And the action of the HttpPost controller:
public ActionResult(MyViewModel viewModel) { ... }
The above will cause the viewModel in the ActionResult to be null. Is there a logical explanation for this? From what I can say, it is specific only for viewing models that inherit from collections.
I know that I can get around it with a custom binder, but the properties associated with it are primitive types, and there aren’t even any generics or inheritance.
I redesigned view models to inherit the type of the collection as properties and fix the problem. However, I am still scratching my head why the binder is breaking on it. Any constructive thoughts appreciated.
source share