MVC 3 Model Binding a Sub Type (Abstract Class or Interface)

This can be achieved through overriding CreateModel(…). I will demonstrate that with an example. 1. Lets create a model and some base and child classes. public class MyModel { public MyBaseClass BaseClass { get; set; } } public abstract class MyBaseClass { public virtual string MyName { get { return “MyBaseClass”; } } } public … Read more

How does a multiple select list work with model binding in ASP.NET MVC?

Yes, by default a multiselectlist will post through an array of the selected values. This article has further information, including how to use strongly-typed views with a multiselectlist. From the linked “article”: Your model or view model class needs a collection property for the IDs for the selected option items, e.g. List<int> ToppingIds. In the … Read more

Passing UTC DateTime to Web API HttpGet Method results in local time

The query string parameter value you are sending 2014-04-01T00:00:00Z is UTC time. So, the same gets translated to a time based on your local clock and if you call ToUniversalTime(), it gets converted back to UTC. So, what exactly is the question? If the question is why is this happening if sent in as query … Read more

How to pass IEnumerable list to controller in MVC including checkbox state?

Use a list instead and replace your foreach loop with a for loop: @model IList<BlockedIPViewModel> @using (Html.BeginForm()) { @Html.AntiForgeryToken() @for (var i = 0; i < Model.Count; i++) { <tr> <td> @Html.HiddenFor(x => x[i].IP) @Html.CheckBoxFor(x => x[i].Checked) </td> <td> @Html.DisplayFor(x => x[i].IP) </td> </tr> } <div> <input type=”submit” value=”Unblock IPs” /> </div> } Alternatively you … Read more

How does MVC 4 List Model Binding work?

There is a specific wire format for use with collections. This is discussed on Scott Hanselman’s blog here: http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx Another blog entry from Phil Haack talks about this here: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx Finally, a blog entry that does exactly what you want here: http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

ASP.NET MVC Binding to a dictionary

In ASP.NET MVC 4, the default model binder will bind dictionaries using the typical dictionary indexer syntax property[key]. If you have a Dictionary<string, string> in your model, you can now bind back to it directly with the following markup: <input type=”hidden” name=”MyDictionary[MyKey]” value=”MyValue” /> For example, if you want to use a set of checkboxes … Read more