我一直試圖弄清楚這一點了一天了,但我只是無法得到這個工作。 我有一個asp.net MVC網站,它的datamodel使用entityframework。 我需要能夠編輯包含一個複雜的Release
實體List<ReleaseDescription>
ASP.net mvc EntityCollection在保存時爲空
我有以下的模型(Apparantly我不能上傳圖片,所以我會只需鍵入出來):
public class Release
{
public string Version
..some other primitive properties
public EntityCollection<ReleaseDescription>
}
public class ReleaseDescription
{
public string Description
public Language Language
}
public class Language
{
public string ISOCode
public string Description
}
看時爲網絡上的這個問題提供解決方案。我發現使用EntityCollection(請參見列表Release.ReleaseDescription
)不是一個好主意,因此在部分類Release
中我做了一個額外的屬性ReleaseDescriptionList
,它通過getter將此entityCollection轉換爲List<ReleaseDescription>
,它沒有setter。
問題是,當保存時,我的release.ReleaseDescription
甚至release.ReleaseDescriptionList
總是空的項目應該在它。
這裏如下我的代碼的其餘部分:
我Edit.aspx代碼看起來像DescriptionRelease這
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Server.DM.Release>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit</h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
... (Code for the primitive properties (works all fine)
<fieldset>
<legend>Descriptions</legend>
<% for(var i =0; i<Model.ListReleaseDescriptions.Count; i++)
{%>
<%: Html.EditorFor(x => Model.ListReleaseDescriptions[i], "ReleaseDescriptionRow")%>
<%} %>
<%= Html.ActionLink("Add another...", "AddDescription", Model) %>
</fieldset>
<p>
<input type="submit" value="Save" />
</p>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
ASP代碼看起來是這樣的:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Server.DM.ReleaseDescription>" %>Language: <%= Html.TextBoxFor(x => x.Language.ISOCode) %>Qty: <%=Html.TextBoxFor(x => x.Description)%>
(我無法在多行上得到上面的代碼塊)對不起)
當我點擊保存按鈕在編輯畫面,我得到我的ActionController
[HttpPost]
public ActionResult Edit(Release release)
release.DescriptionRelease
不包含任何數據時,3項應該是它。 任何幫助解決這個問題表示讚賞!
(PS:是的,我知道有關於這個論壇和其他類似的線程,但它沒有一個似乎爲我工作)