2017-02-24 190 views
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輸入元素也可以正常工作。

+0

參見[這裏](http://stackoverflow.com/questions/25548796/how-to-make-requiredattribute-work-with-an-enum-field) –

+0

感謝你的貢獻。但我沒有得到任何結果通過第二種方法 – Xeta7

+0

任何幫助這個問題? – Xeta7

回答

1

我發現結果可能對他人有所幫助:

當你看到我相關的所謂的「A7O無線電」以單選按鈕,並在這個類類我做單選按鈕顯示:無; 容易嗎?我花了很多時間來了解這一點。因爲顯示器沒有,所以它不顯示客戶端驗證消息。爲了應付它,我爲NotSelected枚舉值插入另一個單選按鈕,並將其不透明度設置爲零;魔法發生了。 我也設置這個單選按鈕的寬度和高度爲零,所以同樣的問題又出現了。

@Html.RadioButtonFor(model => model.KindOfDeal, (int)KindOfDeal.NotSelected,new { @class= "opacity0" })