我创建了一个用于添加/删除列表项的视图。我正在尝试动态添加/删除,但我不清楚当列表为空时如何获得正确的模板上下文。
该观点基于:
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 :(得分:0)
我最终为List<DocumentListItem>
创建了一个名为DocumentListDisplay的显示模板,其中包含以下内容:
<input type="hidden" value="<%: ViewData.TemplateInfo.HtmlFieldPrefix %>" />
我只是在我的视图中调用此模板,然后在同一模型上调用EditorFor()
。它是kludgey,但它提供了模板上下文,无需硬编码任何东西或向ViewData添加任何内容。