2017-02-24 191 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" })