1

我有 Parent 模型“Receipt”,它具有对象字典作为称为CustomControlDictionaryclass的属性CustomControlModel。在我看来,我循环遍历CustomControlDictionary属性中的每个对象并调用 EditorTemplate。在模板中,我只需为我的CustomControlModel对象添加标签和文本框。当代码呈现 html 控件具有相同idname属性时: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所有对象:CustomControlDictionaryReceiptModel

<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
4

1 回答 1

0

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

@model EWMS_MVC.Classes.CustomControlModel
<label>@Model.LabelCaption</label>
<input id="@Model.CustomControlId" name="@Model.CustomControlName" type="text" value="@Model.Value" />
于 2016-01-05T23:51:26.547 回答