ASP.NET MVC 中与嵌套子模型和 PartialView 的模型绑定

发布于 2024-08-25 10:50:16 字数 1226 浏览 7 评论 0原文

我有以下类型和类:

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 并呈现它。

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

在 PartialView 上,我使用强类型 Html Helpers 来执行此操作:

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

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

我尝试使用松散类型的帮助器来克服这个问题:

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

当我处理一个 List 的属性时,它会变得非常难看。然后我必须手动渲染集合索引。

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

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

我缺少什么?

I have the following types and classes:

namespace MVC.Models

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

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

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.

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

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

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

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) %>

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.

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

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

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

迎风吟唱 2024-09-01 10:50:16

我建议您使用 EditorFor 帮助器

模型:

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

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

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>

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) %>

最后是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);
    }
}

I would suggest you to use the EditorFor helper

Model:

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

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

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>

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) %>

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