2011-01-13 65 views
1

我一直試圖弄清楚這一點了一天了,但我只是無法得到這個工作。 我有一個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:是的,我知道有關於這個論壇和其他類似的線程,但它沒有一個似乎爲我工作)

回答

0

OK,很多搜索後,我終於找到了我的問題的答案。

我已經取得額外的屬性來彌補EntityCollection,但它是這樣的:

public IList<ReleaseDescription> ListReleaseDescription 
{ 
    get 
    { 
     return ReleaseDescription.ToList(); 
    } 
} 

那沒有工作,所以我做了一個簡單的屬性,它像這樣:

public IList<ReleaseDescription> ListReleaseDescription{get; set;} 

我在我的控制器中填充了這個屬性。這最終解決了我的問題,並保存了所有數據。當我的解決方案很簡單時,我無法相信我浪費了1.5天的時間。

相關問題