I have a simple Poco model using abstract classes, and it doesn't seem to work with ModelBinder by default for Asp.net MVC 2. One element has several objects in the collection, all of which use the same abstract base class.
Model:
public partial class Item { public virtual ICollection<Core.Object> Objects { get { return _objects; } set { if (value != _objects) { _objects = value; } } } private ICollection<Core.Object> _objects; } public abstract partial class Object { public virtual Item Item { get { return _item; } set { if (!Object.ReferenceEquals(_item, value)) { _item = value; } } } private Item _item; } public partial class TextObject : Object { public virtual string Text { get; set; } }
Instance:
var NewItem = new Item(); var TextObject1 = new TextObject { Text = "Text Object Text", Item = NewItem }; List<Core.Object> objects = new List<Core.Object>(){TextObject1}; NewItem.Objects = objects;
Using the Html.EditorForModel () Default Assistant for this element with one TextObject in the Objects collection, I get this html input field:
<input class="text-box single-line" id="Objects_0__Text" name="Objects[0].Text" type="text" value="Text Object Text" />
When sending back to the controller, I get the message "Unable to create an abstract class" Errormessage from the Default ModelBinder. Obviously, the binder is trying to create this abstract base class. But I donโt know why, since the collection contains only an object of the inherited TextObject type. Is there any other way to get this working without writing a custom Modelbinder?
source share