【问题标题】:Error with expressive annotations requiredif validator如果验证器需要表达性注释,则出错
【发布时间】:2017-06-29 10:36:59
【问题描述】:

这是我的看法

     <div id="Domain">
            <ul class="formlist">
                <li class="width100">
                    <div class="form-check form-check-inline">
                        <label class="form-check-label">
                            @Html.RadioButtonFor(model => model.domain_flag, 1, new { @class = "form-check-input" }) <span>Yes</span>
                        </label>
                    </div>
                    <div class="form-check form-check-inline">
                        <label class="form-check-label">
                            @Html.RadioButtonFor(model => model.domain_flag, 0, new { @class = "form-check-input", @checked = "checked" }) <span>No</span>
                        </label>
                    </div>
                </li>
            </ul>

            <ul class="formlist">
                <li>
                    <div class="frm-marg-b">
                        <label class="label"><b>@Html.LabelFor(model => model.domain_renew_date)</b></label>
                        <div class="textbx">
                            <div class="input-group">
                                @Html.TextBoxFor(model => model.domain_renew_date, new { @type = "datetime", @class = "form-control" })
                                @Html.ValidationMessageFor(model => model.domain_renew_date, "", new { @class = "text-danger" })
                            </div>
                        </div>
                    </div>
                    <div class="frm-marg-b">
                        <label class="label"><b>@Html.LabelFor(model => model.domain_vendor_id)</b></label>
                        <div class="textbx">
                            @Html.DropDownListFor(model => model.domain_vendor_id, Model.domain_Vendor, "Please Select")
                            @Html.ValidationMessageFor(model => model.domain_vendor_id, "", new { @class = "text-danger" })
                        </div>
                    </div>
                </li>
                <li>
                    <div class="frm-marg-b">
                        <label class="label"><b>@Html.LabelFor(model => model.domain_exp_date)</b></label>
                        <div class="textbx">
                            <div class="input-group">
                                @Html.TextBoxFor(model => model.domain_exp_date, new { @type = "datetime", @class = "form-control" })
                                @Html.ValidationMessageFor(model => model.domain_exp_date, "", new { @class = "text-danger" })
                            </div>
                        </div>
                    </div>
                    <div class="frm-marg-b">
                        <label class="label"><b>@Html.LabelFor(model => model.domain_amt)</b></label>
                        <div class="textbx">
                            @Html.EditorFor(model => model.domain_amt, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.domain_amt, "", new { @class = "text-danger" })
                        </div>
                    </div>
                </li>
                <li class="width100">
                    <div class="frm-marg-b">
                        <label class="label"><b>@Html.LabelFor(model => model.domain_remarks)</b></label>
                        <div class="textbx3">
                            @Html.TextAreaFor(model => model.domain_remarks, new { htmlAttributes = new { @class = "form-control", @rows = 2 } })
                            @Html.ValidationMessageFor(model => model.domain_remarks, "", new { @class = "text-danger" })
                        </div>
                    </div>
                </li>
            </ul>
        </div>

我的模特

    public int? domain_flag { get; set; }
    [Display(Name = "Date of Renewal")]
    [DataType(DataType.Date)]
    [RequiredIf("domain_flag==1",ErrorMessage ="Enter Renew Date")]
    public DateTime? domain_renew_date { get; set; }
    [Display(Name = "Date of Expiry")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:mm/dd/yy}", ApplyFormatInEditMode = true)]
    [RequiredIf("domain_flag==1", ErrorMessage = "Enter Expiry Date")]
    public DateTime? domain_exp_date { get; set; }
    [Display(Name = "Vendor")]
    [RequiredIf("domain_flag==1", ErrorMessage = "Please Select Vendor")]
    public int? domain_vendor_id { get; set; }
    [Display(Name = "Amount(Rs.)")]
    [RequiredIf("domain_flag==1", ErrorMessage = "Enter Amount")]
    [RegularExpression("^[0-9]+$", ErrorMessage = "Enter Numeric Values")]
    public decimal? domain_amt { get; set; }
    [Display(Name = "Comments")]        
    public string domain_remarks { get; set; }

全球.asax

                     DataAnnotationsModelValidatorProvider.RegisterAdapter(
            typeof(RequiredIfAttribute), typeof(RequiredIfValidator));
        DataAnnotationsModelValidatorProvider.RegisterAdapter(
            typeof(AssertThatAttribute), typeof(AssertThatValidator));

在我选择单选按钮后,如果在输入值之前显示错误消息,则需要。在发布到服务器之前,应在单击提交按钮时显示错误消息。这可能与富有表现力的注释 nuget 包吗?

【问题讨论】:

标签: model-view-controller data-annotations expressiveannotations


【解决方案1】:

是的,可以使用 ExpressiveAnnotations。

@jwaliszko 提到了它。

你必须在客户端这样设置 ea 的配置:

<script>
    ea.settings.dependencyTriggers = ''; 

【讨论】:

  • 我应该在 global.asax 哪里写这个?
  • 你必须把它写在你的cshtml或html文件中。在脚本部分
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-08
  • 2023-02-03
相关资源
最近更新 更多