【发布时间】:2014-05-12 09:54:35
【问题描述】:
我正在尝试对复杂属性使用编辑器模板,但模板中的字段默认设置为要求用户填写。有没有一种简单的方法可以避免这种情况并仍然使用“editorfor” ?我知道有一个 [required] 数据注释,但我没有使用它,也找不到相反的命令。模板显示正确,我只是不希望这些字段是必需的。
我的视图模型中的属性:
[UIHint("EditPriceInfo")]
[Display(Name="Price info")]
public PriceInfo PriceInfo { get; set; }
来自模板文件“/Views/Shared/EditorTemplates/EditPriceInfo.cshtml”。 (通过“PriceInfo”类的脚手架创建,见下文。):
@model MyProject.Models.PriceInfo
<div class="form-group">
@Html.LabelFor(model => model.HourPrice, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.HourPrice)
@Html.ValidationMessageFor(model => model.HourPrice)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DayPrice, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DayPrice)
@Html.ValidationMessageFor(model => model.DayPrice)
</div>
</div>
从“编辑”视图调用模板:
<h4>@Html.DisplayNameFor(model => model.PriceInfo)</h4>
<div class="form-group">
<div class="col-md-10">
@Html.EditorFor(model => model.PriceInfo)
@Html.ValidationMessageFor(model => model.PriceInfo)
</div>
</div>
以及原始“PriceInfo”实体中的属性:
[Display(Name = "Price/hour")]
public decimal HourPrice { get; set; }
[Display(Name = "Price/day")]
public decimal DayPrice { get; set; }
【问题讨论】:
标签: asp.net-mvc-5