我有 Parent 模型“Receipt”,它具有对象字典作为称为CustomControlDictionary
class的属性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 })
渲染的 html 具有相同的id
和属性中的name
所有对象:CustomControlDictionary
ReceiptModel
<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