1
,枚举单选按钮工作,我有一个枚举:客户端验证不MVC
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输入元素也可以正常工作。
参见[这里](http://stackoverflow.com/questions/25548796/how-to-make-requiredattribute-work-with-an-enum-field) –
感谢你的贡献。但我没有得到任何结果通过第二种方法 – Xeta7
任何帮助这个问题? – Xeta7