【问题标题】:Client side validation does not work for enum radio button in mvc客户端验证不适用于 mvc 中的枚举单选按钮
【发布时间】:2017-02-24 17:33:18
【问题描述】:

我有一个枚举:

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 输入元素,除了这些枚举单选按钮。

【问题讨论】:

  • here
  • 感谢您的贡献。但是我没有通过第二种方法得到任何结果
  • 对这个问题有帮助吗?

标签: asp.net-mvc


【解决方案1】:

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

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

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

【讨论】:

  • 是的,我今天也注意到,如果您希望客户端验证正常工作,您不能在 &lt;input type="radio" /&gt; 元素上使用 display:none
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-12
  • 2011-02-06
  • 1970-01-01
  • 1970-01-01
  • 2011-01-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多