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