[英]Model binding with nested child models and PartialViews in ASP.NET MVC
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. 我想在Edit.aspx
视图中编辑Page
实例。 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. 因为EditableContent
也附加到其他模型,所以我有一个名为ContentEditor.ascx
的PartialView
,它是强类型的,并获取EditableContent
的实例并呈现它。
The rendering part all works fine, but when I post - everything inside my ContentEditor
is not binded - which means that Page.Content
is null
. 渲染部分一切正常,但是当我发布时 - 我的ContentEditor
内的所有内容都没有绑定 - 这意味着Page.Content
为null
。
On the PartialView, I use strongly typed Html Helpers to do this: 在PartialView上,我使用强类型的Html Helpers来执行此操作:
<%= 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
. 但是因为ContentEditor.ascx
呈现的表单上的输入元素没有获得其id
属性的Content
前缀 - 这些值不会绑定到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. 当我处理一个属于List<T>
的属性时,它变得非常难看。 I then have to render collection indexes manually. 然后我必须手动渲染集合索引。
Should I put both Page and EditableContent as parameters to the controller action?: 我应该将Page和EditableContent作为参数添加到控制器操作中吗?:
public ActionResult Edit(Page page, EditableContent content) { ... }
What am I missing? 我错过了什么?
I would suggest you to use the EditorFor
helper 我建议你使用EditorFor
帮助器
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: 查看/首页/的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: 查看/共享/ 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: 最后是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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.