Reputation: 2255
I have a model, something like:
public class PageModel {
public string PropA { get; set; }
public string PropB { get; set; }
public Type DataType { get; set; }
public PageModel() {}
...
}
My view then has something like:
...
@using (Html.BeginForm()) {
<li>@Html.LabelFor(m => m.PropA) <br /> @Html.TextBoxFor(m => m.PropA)</li>
<li>@Html.LabelFor(m => m.PropB) <br /> @Html.TextBoxFor(m => m.PropB)</li>
<li>@Html.LabelFor(m => m.DataType) <br /> @Html.TextBoxFor(m => m.DataType)</li>
<li><input type="submit" /></li>
}
...
Using default model binding, the String properties get set correctly, but the Type property ends up as NULL, even when provided with a valid Type string representation (e.g. "System.String" or "System.Boolean").
Any recommendations on how to get this Type property bound correctly? A type-specific custom model binder seems like the best approach, but I wouldn't want to rewrite the binding of PropA and PropB.
Upvotes: 0
Views: 87
Reputation: 2255
Per Ant P's comment, I implemented a model binder which inherits from DefaultModelBinder, and overrode BindProperty()
See http://dotnetslackers.com/articles/aspnet/Understanding-ASP-NET-MVC-Model-Binding.aspx#s11-custom-binding-with-only-few-properties for details.
Thanks Ant P!
Upvotes: 1