MVC3 Razor 模型绑定器和继承集合

发布于 2024-11-16 09:12:54 字数 994 浏览 1 评论 0原文

我希望我在这里没有遗漏一些非常明显的东西,但是模型绑定器在绑定从集合继承的视图模型时总是遇到麻烦,有什么原因吗?

假设我想显示一个分页列表并显示一个组合框并在其上方添加按钮(处理简单列表)。涉及的类看起来像:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹过旳痕迹 2024-11-23 09:12:54

回答我自己的问题:我所有与集合有关的模型不再从通用列表或类似的继承,而是具有所需集合类型的属性。这效果更好,因为渲染时您可以使用

@Html.EditorFor(m => m.CollectionProperty)

并在 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

@Html.EditorFor(m => m.CollectionProperty)

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.

请爱~陌生人 2024-11-23 09:12:54

有时,如果表单帖子中的数据采用不同的格式,则模型绑定到集合的效果会更好。

我使用一个名为 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文