-1

I have a viewmodel consisting of a handful of properties:

public class FooDataViewModel : IValidatableObject
{
/* Several Properties working as expected */

public ICollection<Foo> Foos {get; set;}

/* Validation working as expected */
}

Foo holds a few fields and works as expected:

public class Foo
{
/* Only a few basic properties, works as expected */
}

But I have some objects that are Foobar:

public class FooBar : Foo, IValidatableObject
{
/* Just a few more properties and some conditional validation */
}

I created a custom editor template for both Foo and FooBar. For my edit view, I simply use '@Html.EditorFor(m => m.Foos)' and editors for both Foo and FooBar are displayed. But when I submit to my HttpPost method, only Foo objects are created, and the FooBar specific data is lost. How can I make sure that that the FooBar objects are created?

4

1 回答 1

1

如何确保创建了 FooBar 对象?

您将需要在请求中包含此信息并编写能够使用此信息并实例化正确类型的自定义模型绑定器。我在这里说明了这个概念:ViewModel with List<BaseClass> and editor templates

于 2013-07-29T16:15:37.517 回答