MVC3 Razor 模型绑定器和继承集合
我希望我在这里没有遗漏一些非常明显的东西,但是模型绑定器在绑定从集合继承的视图模型时总是遇到麻烦,有什么原因吗?
假设我想显示一个分页列表并显示一个组合框并在其上方添加按钮(处理简单列表)。涉及的类看起来像:
public class PagedList<T> : List<T>
{
public int TotalCount { get; set; }
}
然后视图模型看起来像:
public class MyViewModel : PagedList<ConcreteModel>
{
public IEnumerable<ChildModel> List { get; set; }
public int? SelectedChildModelId { get; set; }
}
所以在视图(Razor)中:
@model MyViewModel
@using (Html.BeginForm())
{
@Html.DropDownListFor(model => model.SelectedChildModelId, new SelectList(Model.List, "ChildModelId", "DisplayName"))
}
控制器 HttpPost 操作:
public ActionResult(MyViewModel viewModel)
{
...
}
上面将导致 ActionResult 中的 viewModel 为 null。有一个合乎逻辑的解释吗?据我所知,它仅特定于查看从集合继承的模型。
我知道我可以使用自定义绑定器来解决它,但涉及的属性是原始类型,甚至没有任何泛型或继承。
我重新设计了视图模型,将集合继承类型作为属性,从而解决了问题。然而,我仍然不明白为什么活页夹会在上面破裂。任何建设性的想法表示赞赏。
I hope I'm not missing something incredibly obvious here but is there any reason why model binder is always having trouble binding a view model that inherits from a collection?
Lets say I want to show a paged list and display a combo box and add button above it (dealing with simple lists). Involved classes would look like:
public class PagedList<T> : List<T>
{
public int TotalCount { get; set; }
}
And then a view model that looks like:
public class MyViewModel : PagedList<ConcreteModel>
{
public IEnumerable<ChildModel> List { get; set; }
public int? SelectedChildModelId { get; set; }
}
So in the view (Razor):
@model MyViewModel
@using (Html.BeginForm())
{
@Html.DropDownListFor(model => model.SelectedChildModelId, new SelectList(Model.List, "ChildModelId", "DisplayName"))
}
And the controller HttpPost action:
public ActionResult(MyViewModel viewModel)
{
...
}
The above will cause viewModel in ActionResult to be null. Is there a logical explanation for it? From what I can tell it's specific only to view models that inherit from collections.
I know I can get around it with custom binder but the properties involved are primitive types and there isn't even any generics or inheritance.
I've reworked the view models to have the collection inherited type as properties and that fixes the issue. However I'm still scratching my head over why the binder breaks down on it. Any constructive thoughts appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
回答我自己的问题:我所有与集合有关的模型不再从通用列表或类似的继承,而是具有所需集合类型的属性。这效果更好,因为渲染时您可以使用
并在 Views/Shared/EditorTemplates 下为包含的类型创建自定义编辑器。它还可以与模型绑定器完美配合,因为集合中的所有单个项目都会获得一个索引,并且绑定器能够在提交时自动绑定它。
经验教训:如果您计划在视图中使用模型,请不要从集合类型继承。
To answer my own question: All my models that have anything to do with collections no longer inherit from generic list or similar but instead have a property of the required collection type. This works much better because when rendering you can use
And create a custom editor under Views/Shared/EditorTemplates for contained type. It also works beautifully with model binder since all individual items from collection get a index and the binder is able to auto bind it when submitted.
Lesson learned: if you plan to use models in views, don't inherit from collection types.
有时,如果表单帖子中的数据采用不同的格式,则模型绑定到集合的效果会更好。
我使用一个名为 postify 的插件。
http://www.nickriggs。 com/posts/post-complex-javascript-objects-to-asp-net-mvc-controllers/
Sometimes model binding to a collection works better if the data in the form post is formatted differently.
I use a plugin called postify.
http://www.nickriggs.com/posts/post-complex-javascript-objects-to-asp-net-mvc-controllers/