客户端验证不适用于mvc中的枚举单选按钮

时间:2017-02-24 17:33:19

标签: asp.net-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输入元素EXCEPT这些枚举单选按钮。

1 个答案:

答案 0 :(得分:1)

我发现结果对其他人有帮助:

如你所见,我联系了一个名为&#34; A7O-radio&#34;单选按钮,在这个类中我制作单选按钮显示:无; 简单?我花了很多时间来了解这一点。因为显示为none,所以它不显示客户端验证消息。为了解决这个问题,我为NotSelected枚举值插入了另一个单选按钮,并使其不透明度为零;魔法发生了。 我还将此单选按钮的宽度和高度设置为零,因此再次出现同样的问题。

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