MVC 5 EditorTemplate for Model渲染具有相同的id和名称

时间:2016-01-05 21:53:41

标签: asp.net-mvc editortemplates

我有父模型"收据"并且它将对象定义为类CustomControlDictionary的{​​{1}}属性。 在我看来,我遍历CustomControlModel属性中的每个对象并调用EditorTemplate。在模板中,我只需为我的CustomControlDictionary对象添加标签和文本框。 代码呈现时,html控件具有相同的CustomControlModelid属性:name id="key_Value"。 我确实尝试使用regualr数组或列表而不是字典,但结果相同。期待任何建议。

型号:

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 }) id属性中的所有对象,呈现的html具有相同的nameCustomControlDictionary

ReceiptModel

1 个答案:

答案 0 :(得分:0)

我认为这种情况的最佳方法是避免模板中的HtmlHelper方法,而是使用html控件:

@model EWMS_MVC.Classes.CustomControlModel
<label>@Model.LabelCaption</label>
<input id="@Model.CustomControlId" name="@Model.CustomControlName" type="text" value="@Model.Value" />