【发布时间】:2016-01-05 21:53:41
【问题描述】:
我有父模型“Receipt”,它具有对象的字典作为一个名为 CustomControlDictionary 的属性,属于 CustomControlModel 类。
在我看来,我遍历CustomControlDictionary 属性中的每个对象并调用EditorTemplate。在模板中,我只需为 CustomControlModel 对象添加标签和文本框。
当代码呈现 html 控件具有相同的 id 和 name 属性: id="key_Value" name="key.Value" 。
我确实尝试使用常规数组或列表而不是字典,但结果相同。期待任何建议。
型号:
public class ReceiptModel
{
public ReceiptModel(){}
public int ReceiptId { get; set; }
public string ReceiptNumber { get; set; }
public Dictionary<string, CustomControlModel> CustomControlDictionary{get; set; }
// public List<CustomControlModel> CustomControlList { get; set; }
}
public class CustomControlModel
{
public CustomControlModel(){}
public int CustomControlId{ get; set; }
public string CustomControlName{ get; set; }
public string LabelCaption{ get; set; }
public string Value{ get; set; }
}
查看:
//View (@ReceiptModel):
@foreach (var key in Model.CustomControlDictionary )
{
@Html.EditorFor(m => key.Value,"CustomControlModel")
}
编辑器模板:
@model EWMS_MVC.Classes.CustomControlModel
@Html.HiddenFor(model => model.CustomControlId)
@Html.LabelFor(model => model.CustomControlId, Model.LabelCaption)
@Html.TextBoxFor(model => model.CustomControlId, new { @Value = @Model.Value })
对于ReceiptModel 的CustomControlDictionary 属性中的所有对象,呈现的html 具有相同的id 和name:
<input id="key_Value_CustomControlId" name="key.Value.CustomControlId" type="hidden" value="201922" />//value here should be "id" of the input field
<label for="key_Value_CustomControlId">Receipt number:</label>
<input id="key_Value_CustomControlId" name="key.Value.CustomControlId" type="text" value="201922" />//value here should be "id" of the input field
【问题讨论】:
标签: asp.net-mvc editortemplates