MVC 中模型绑定期间属性上的 KnownType 属性?
假设我有一个 Food 类和一个派生自 Food 的 BirdFood 类:
public class Food
{
...
}
public class BirdFood : Food
{
public int SeedCount { get; set; }
}
假设我有一个 Creature 类和一个派生自 Creature 的 Bird 类
public class Creature
{
public Food Food { get; set; }
}
public class Bird : Creature
{
...
}
最后,假设我有一个 Creature 的视图模型:
public class ViewModel
{
public Creature Creature { get; set; }
}
当我创建一个实例时对于 Bird,我还创建了 BirdFood 的实例并将其分配给 Food 属性,并将视图模型传递给 View。够简单的。
public ActionResult Index()
{
ViewModel viewModel = new ViewModel();
viewModel.Creature = new Bird();
viewModel.Creature.Food = new BirdFood() { SeedCount = 100 } ;
return View(viewModel);
}
观点:
<% using(Html.BeginForm("Save", "Home", FormMethod.Post))
{ %>
<%: Html.Hidden("Creature.Food.SeedCount", (Model.Creature.Food as MvcApplication6.Controllers.BirdFood).SeedCount) %>
<input type="submit" />
<% } %>
好吧,问题就在这里。当 Action 中接收到视图模型时,Creature 属性是基类 Creature,而不是派生类型 Bird,其 SeedCount 属性是派生类型 BirdFood,其 post 参数(来自 firebug)看起来像 Creature。 Food.SeedCount 100
好的,我的问题是,当模型绑定?我怀疑它与自定义模型绑定器有关,但对此我一无所知。有什么想法吗?
Lets say I have the class Food and a class BirdFood that derives from Food:
public class Food
{
...
}
public class BirdFood : Food
{
public int SeedCount { get; set; }
}
And lets say I have a class Creature and a class Bird that derives from Creature
public class Creature
{
public Food Food { get; set; }
}
public class Bird : Creature
{
...
}
And finally, lets say I have a view model for Creature:
public class ViewModel
{
public Creature Creature { get; set; }
}
When I create an instance of Bird, I also create an instance of BirdFood and assign it to the Food property and pass the view model to the View. Simple enough.
public ActionResult Index()
{
ViewModel viewModel = new ViewModel();
viewModel.Creature = new Bird();
viewModel.Creature.Food = new BirdFood() { SeedCount = 100 } ;
return View(viewModel);
}
And the view:
<% using(Html.BeginForm("Save", "Home", FormMethod.Post))
{ %>
<%: Html.Hidden("Creature.Food.SeedCount", (Model.Creature.Food as MvcApplication6.Controllers.BirdFood).SeedCount) %>
<input type="submit" />
<% } %>
Okay, here is the problem. When the view model is received in the Action, the Creature property is of the base class Creature instead of the derived type Bird with the SeedCount property of the derived type BirdFood with the post parameters (from firebug) that looks like Creature.Food.SeedCount 100
Okay, my questions is, how do I preserve the derived types when model binded? I'm suspecting it has to do with a custom Model Binder but I'm clueless when it comes to that. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认模型绑定器只是检查视图模型中的类型并使用这些类。他无法猜测您想要某个派生类。可以使用自定义模型绑定器来提供此类提示。这是另一个。
The default model binder simply inspects the types in your view model and uses those classes. He cannot guess that you want some derived class. A custom model binder could be used to provide such hint. And here's another one.