ASP.NET MVC 中嵌套子模型和 PartialViews 的模型绑定 [英] Model binding with nested child models and PartialViews in ASP.NET MVC

查看:38
本文介绍了ASP.NET MVC 中嵌套子模型和 PartialViews 的模型绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类型和类:

namespace MVC.Models

public class Page 
{
   public EditableContent Content {get; set; }
}

public class EditableContent
{
    public TemplateSection SidebarLeft {get; set; }
    public TemplateSection SidebarRight {get; set; }
}

我想在我的 Edit.aspx 视图中编辑 Page 实例.由于 EditableContent 也附加到其他模型,我有一个名为 ContentEditor.ascxPartialView,它是强类型的并采用 的实例EditableContent 并呈现它.

I want to edit the Page instance in my Edit.aspx View. Because EditableContent is also attached to other models, I have a PartialView called ContentEditor.ascx that is strongly typed and takes an instance of EditableContent and renders it.

渲染部分一切正常,但是当我发布时 - 我的 ContentEditor 中的所有内容都没有绑定 - 这意味着 Page.Contentnull.

The rendering part all works fine, but when I post - everything inside my ContentEditor is not binded - which means that Page.Content is null.

在 PartialView 上,我使用强类型 Html Helpers 来做到这一点:

On the PartialView, I use strongly typed Html Helpers to do this:

<%= Html.HiddenFor(m => m.TemplateId) %>

但是因为表单上由 ContentEditor.ascx 呈现的输入元素没有获得 Content 前缀到其 id 属性 -值未绑定到 Page.

But because the input elements on the form that are rendered by ContentEditor.ascx does not get the Content prefix to its id attribute - the values are not binded to Page.

我尝试使用松散类型的助手来解决这个问题:

I tried using loosely typed helpers to overcome this:

<%= Html.Hidden("Content.TemplateId", Model.TemplateId) %>

当我处理一个属性是List 时,它会变得非常难看.然后我必须手动呈现集合索引.

And when I'm dealing with a property that is a List<T> of something it gets very ugly. I then have to render collection indexes manually.

我应该将 Page 和 EditableContent 作为参数放入控制器操作吗?:

Should I put both Page and EditableContent as parameters to the controller action?:

public ActionResult Edit(Page page, EditableContent content) { ... }

我错过了什么?

推荐答案

我建议你使用 EditorFor helper

I would suggest you to use the EditorFor helper

型号:

public class EditableContent
{
    public string SidebarLeft { get; set; }
    public string SidebarRight { get; set; }
}

public class Page
{
    public EditableContent Content { get; set; }
}

视图/主页/Index.aspx:

Views/Home/Index.aspx:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ToDD.Models.Page>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm()) { %>
    <%-- 
    This is the important part: It will look for 
    Views/Shared/EditorTemplates/EditableContent.ascx
    and render it. You could also specify a prefix
    --%>
    <%= Html.EditorFor(page => page.Content, "Content") %>
    <input type="submit" value="create" />
<% } %>
</asp:Content>

视图/共享/EditorTemplates/EditableContent.ascx:

Views/Shared/EditorTemplates/EditableContent.ascx:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ToDD.Models.EditableContent>" %>

<%= Html.TextBoxFor(m => m.SidebarLeft) %>
<br/>
<%= Html.TextBoxFor(m => m.SidebarRight) %>

最后是控制器/HomeController:

And finally Controller/HomeController:

public class HomeController : Controller
{
    public ActionResult Edit()
    {
        var page = new Page
        {
            Content = new EditableContent
            {
                SidebarLeft = "left",
                SidebarRight = "right"
            }
        };
        return View(page);
    }

    [HttpPost]
    public ActionResult Edit(Page page)
    {
        return View(page);
    }
}

这篇关于ASP.NET MVC 中嵌套子模型和 PartialViews 的模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆