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

问题描述

我有以下类型和类:

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 查看。因为 EditableContent 也连接到其它车型,我有一个 PartialView 名为 ContentEditor.ascx 这是强类型,并采取 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.Content

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辅助方法做到这一点:

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

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

但因为是由 ContentEditor.ascx 渲染表单输入元素没有得到内容 preFIX其 ID 属性 - 值不绑定到

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

当我处理一个属性,它是一个名单,其中,T&gt;将东西它变得非常难看。然后,我必须手动渲染集合索引。

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.

我应该把页面和EditableContent作为参数传递给控制器​​的动作:

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

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

我在想什么?

What am I missing?

推荐答案

我会建议你使用 EditorFor 帮助

型号:

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);
    }
}

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

08-11 22:16