【发布时间】:2017-02-24 17:33:18
【问题描述】:
我有一个枚举:
public enum KindOfDeal
{
NotSelected,
BuyIt,
SaleIt,
RentIt,
WantRent,
ExchangeIt}
我在我的模型中使用这个枚举如下:
public class Property : IProperty
{
[Key]
public virtual int PropertyId { set; get; }
[Range(minimum: 0, maximum: double.MaxValue, ErrorMessage = "Essential")]
public virtual double? LandArea { set; get; }
[Range(minimum: 0, maximum: double.MaxValue, ErrorMessage = "Essential")]
public virtual double? FoundationArea { set; get; }
[Required(ErrorMessage = "Essential")]
public virtual KindOfDeal? KindOfDeal { set; get; }
}
然后我使用此枚举在 cshtml 文件中创建单选按钮,如下所示:
<div id="kind-of-deal">
@Html.ValidationMessageFor(model => model.KindOfDeal, null, htmlAttributes: new { @class = "ErrorMessageForInputs" })
<div class="A7O-wrapr-radio">
@Html.RadioButtonFor(model => model.KindOfDeal, (int)KindOfDeal.BuyIt, htmlAttributes: new { @class = "A7O-radio", id = "p-purchase" })
@Html.Label("p-purchase", "", new { @class = "A7O-label-radio", data_icon = " " })
</div>
<div class="A7O-wrapr-radio">
@Html.RadioButtonFor(model => model.KindOfDeal, (int)KindOfDeal.SaleIt, htmlAttributes: new { @class = "A7O-radio", id = "p-sale" })
@Html.Label("p-sale", "", new { @class = "A7O-label-radio", data_icon = " " })
</div>
<div class="A7O-wrapr-radio rel" id="parent-rentPresenter">
@Html.RadioButtonFor(model => model.KindOfDeal, (int)KindOfDeal.RentIt, htmlAttributes: new { @class = "A7O-radio", id = "p-rent-presenter" })
@Html.Label("p-rent-presenter", "", new { @class = "A7O-label-radio", data_icon = " " })
</div>
<div class="A7O-wrapr-radio rel">
@Html.RadioButtonFor(model => model.KindOfDeal, (int)KindOfDeal.WantRent, htmlAttributes: new { @class = "A7O-radio", id = "p-rent-wanted" })
@Html.Label("p-rent-wanted", "", new { @class = "A7O-label-radio", data_icon = " " })
</div>
<div class="A7O-wrapr-radio">
@Html.RadioButtonFor(model => model.KindOfDeal, (int)KindOfDeal.ExchangeIt, htmlAttributes: new { @class = "A7O-radio", id = "p-exchange-presenter" })
@Html.Label("p-exchange-presenter", "", new { @class = "A7O-label-radio", data_icon = " " })
</div>
<div class="A7O-wrapr-radio">
validate.js 和 validate.unobtrusive.js 在我的 cshtml 文件中被引用。此外,我将此枚举设为可为空的必需属性。服务器端验证适用于这些枚举单选按钮。此外,客户端验证适用于其他 html 输入元素,除了这些枚举单选按钮。
【问题讨论】:
-
见here
-
感谢您的贡献。但是我没有通过第二种方法得到任何结果
-
对这个问题有帮助吗?
标签: asp.net-mvc