ASP.Net MVC 模型绑定
我有一个可以正确绑定以用于显示目的的模型,但我无法发布更新版本。
我可以在表单中显示页面及其内容列表,以便在强类型视图中进行更新。
但是,当我发布更新的模型时,即如果我更改了页面内容内容,则发布的模型包含页面详细信息,但丢失了页面和页面内容之间的关系。
因此,当我获取用于显示目的的页面时,展开的 PageContents 页面包含正确的详细信息。
但是当我更新并且发布页面内容时不包含页面条目。
(顺便说一句,可能是转移注意力,但我正在使用 Fluent nHibernate)
不幸的是我找不到我缺少的东西。
这是我的代码的删节版本:
public class Page
{
public virtual int Id { get; private set; }
public virtual string Name { get; set; }
public virtual string Title { get; set; }
public virtual IList<PageContent> PageContents { get; private set; }
public Page()
{
PageContents = new List<PageContent>();
}
public virtual void AddPageContent(PageContent pageContent)
{
pageContent.Page = this;
PageContents.Add(pageContent);
}
}
public class PageContent
{
public virtual int Id { get; private set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual string Content { get; set; }
public virtual Page Page { get; set; }
}
Page <%= Html.LabelFor(model => model.Title) %>:
<%= Html.TextBoxFor(model => model.Title, new { @class = "wide-input" })%>
<%= Html.ValidationMessageFor(model => model.Title) %>
Page <%= Html.LabelFor(model => model.Name) %>:
<%= Html.TextBoxFor(model => model.Name, new { @class = "wide-input" })%>
<%= Html.ValidationMessageFor(model => model.Name) %>
<%
foreach (var item in Model.PageContents)
{
%>
<%= Html.TextAreaFor(m => item.Content) %>
<%
}
%>
<p><input type="submit" value="Save" /></p>
<% } %>
I have a Model that I can bind correctly for display purposes but I can't post an updated version.
I can display a page and its list of contents in a form for updating in a strongly typed view.
But when I post an updated model i.e. if I changed a pagecontents content then the posted model contains the Pages details but loses the relationship between the page and the pagecontent.
So when I get the Page for display purposes the expanded PageContents Page contains the correct deatils.
But when I update and Post PageContents contains no Page entry.
(BTW may be a red herring but I am using Fluent nHibernate)
Unfortunately I can't find what I am missing.
So here's an abridged version of my code:
public class Page
{
public virtual int Id { get; private set; }
public virtual string Name { get; set; }
public virtual string Title { get; set; }
public virtual IList<PageContent> PageContents { get; private set; }
public Page()
{
PageContents = new List<PageContent>();
}
public virtual void AddPageContent(PageContent pageContent)
{
pageContent.Page = this;
PageContents.Add(pageContent);
}
}
public class PageContent
{
public virtual int Id { get; private set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual string Content { get; set; }
public virtual Page Page { get; set; }
}
Page <%= Html.LabelFor(model => model.Title) %>:
<%= Html.TextBoxFor(model => model.Title, new { @class = "wide-input" })%>
<%= Html.ValidationMessageFor(model => model.Title) %>
Page <%= Html.LabelFor(model => model.Name) %>:
<%= Html.TextBoxFor(model => model.Name, new { @class = "wide-input" })%>
<%= Html.ValidationMessageFor(model => model.Name) %>
<%
foreach (var item in Model.PageContents)
{
%>
<%= Html.TextAreaFor(m => item.Content) %>
<%
}
%>
<p><input type="submit" value="Save" /></p>
<% } %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上,每个列表元素都需要唯一的 ID。 这样的事情
可以通过以下方式完成:
此博客文章:
http://weblogs.asp.net/nmarun/archive/2010/03/13/asp-net-mvc-2-model-binding-for-a- collection.aspx
这篇博文描述了一个更好的解决方案:
http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
它使用自己的扩展 HTML Helper 方法来初始化列表绑定。
编辑:我误解了这一点,专注于 ASP.NET MVC 的集合:
据我所知,MVC 无法恢复这种关系。如果回发后页面已包含 PageContent,则一切都“正常”。意思是:您的收藏已正确发布回来。但 MVC 不知道您的 PageContent.Page 引用。所以你必须手动恢复。
编辑2:
如果我解释正确,您将直接使用您的模型来绑定您的视图 - 对吗?如果是这样,我建议您创建 ViewModel,从模型中复制所有内容并返回。这会将模型与视图分离。您的 ViewModel 可以提供您的 View 所需的更多信息。请参阅 MVVM 模式。
Basically you need unique ID's for each list element. Something like
This can be done by:
This this blogpost:
http://weblogs.asp.net/nmarun/archive/2010/03/13/asp-net-mvc-2-model-binding-for-a-collection.aspx
This blogpost describes a much better solution:
http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
It uses an own extensions HTML Helper method to initialize the list bindings.
EDIT: I misunderstood this, was focused on Collections with ASP.NET MVC:
As far as I know MVC cant restore this relationship. If Page already contains PageContent after a PostBack then everything is "fine". Means: Your collections are posted correctly back. But MVC has no idea about you PageContent.Page reference. So you have to restore if manually.
EDIT2:
If I interpret that correctly you are using your model directly to bind your views - right? If so, I would suggest that you create ViewModels, copying everything from your model and back. This would decouple the Model from the View. Your ViewModel could provide much more information needed by your View. See MVVM Pattern.