【发布时间】:2018-07-11 08:41:14
【问题描述】:
我有 Property 表,它有 Id 和 Name。
我需要为这些属性动态地编写一个表单。
我尝试过这样的事情:
@foreach (var item in Model.Properties) {
<div class="col-md-6 margin_top_15">
@Html.LabelFor(m => item.Name, new {@class = "col-md-2 control-label"})
<div class="col-md-10">
@Html.TextBoxFor(m => item.Name, new {@class = "form-control"})
@Html.ValidationMessageFor(m => item.Name, "", new {@class = "text-danger"})
</div>
</div>
}
问题是我得到这样的形式:
我明白为什么会发生这种情况,因为属性名称总是相同的名称 - “名称”。
型号是:
public class PropertyField {
[Key]
public int FieldId { get; set; }
public string FieldName { get; set; }
public string FieldValue { get; set; }
}
我可以将 LabelFor 更改为 Label 并获得良好的字段外观,但提交模型为空:
@Html.Label(item.FieldName, new {@class = "col-md-2 control-label"})
【问题讨论】: