2010-08-06 29 views
-1

我創建了添加/刪除列表項的視圖。我試圖處理添加/刪除動態,但我不清楚如何獲得正確的模板上下文時,清單是空的。空列表的模板/數據/模型上下文ASP.NET MVC2

的觀點是基於:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Service.Cms.ListItems; 

namespace Bcp.Service.Cms.Documents 
{ 
    public class Tasting //: Document 
    { 
     public Tasting() { } 

     public List<DocumentListItem> relatedCrap { get; set; } 
    } 
} 

相關貨品看起來是這樣的:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Service.Cms.ListItems 
{ 
    public class DocumentListItem 
    { 
     public DocumentListItem() { } 

     public Int32 documentId { get; set; } 

     public String title { get; set; } 

     public String documentType { get; set; } 

     public String status { get; set; } 
    } 
} 

爲了保存列表項,每個屬性都需要與tastingInfo.relatedDocs.#PROPERTYNAME#前綴 但如果列表爲空時,我加載視圖,我不知道如何訪問該上下文。任何人有任何想法?

回答

0

我結束了創建一個List<DocumentListItem>顯示模板稱爲DocumentListDisplay包含以下內容:

<input type="hidden" value="<%: ViewData.TemplateInfo.HtmlFieldPrefix %>" /> 

我只是叫我查看此模板在同一個模型調用EditorFor()之前。這是kludgey,但它提供了模板上下文,而不必硬編碼任何東西或向ViewData添加任何內容。

相關問題