【问题标题】:ASP.Net How remove/ignore validation from specific elements upon submit?ASP.Net 如何在提交时从特定元素中删除/忽略验证?
【发布时间】:2019-08-13 13:59:21
【问题描述】:

我有一个带有两个单选按钮的简单表单的 ASP.Net Core 项目。一个单选按钮有一个下拉菜单,另一个有一个文本框。如果用户选择第一个单选按钮,我想在用户按下提交按钮时删除/忽略文本框的验证(因为它是空的)。反之亦然,如果用户选择第二个选项,我想从下拉列表中删除/忽略验证。我尝试了一堆在 StackOverflow 上找到的 jQuery 方法,但没有一个有效。

型号:

  public class EnrollmentModel : BaseModel
  {
    ...
    public bool PriceOption { get; set; }

    [Required(ErrorMessage = "Please select a rate plan")]
    public string SelectedRatePlan { get; set; }

    public IEnumerable<SelectListItem> RatePlans { get; set; }

    [Required]
    public string CustomRate { get; set; }
    ...
    }
  }

查看:

  <div class="form-group">
    <div class="row">
      @Html.RadioButtonFor(x => Model.PriceOption, true, htmlAttributes: new { @id = "comed", @class = "col-md-1" })
      @Html.Label("Use price from current rate plans")
      @Html.DropDownListFor(x => Model.SelectedRatePlan, new SelectList(Model.RatePlans, "Value", "Text"), htmlAttributes: new { @class = "form-control", id = "rateplans", style = "width:100px;", required = "Select Rate Plan" })
      @Html.ValidationMessageFor(x => Model.SelectedRatePlan, "", new { @class = "text-danger" })

    </div>
  </div>
  <div class="form-group">
    <div class="row">
      @Html.RadioButtonFor(x => Model.PriceOption, false, htmlAttributes: new { @id = "custom", @class = "col-md-1" })
      @Html.Label("Enter custom price")
      @Html.TextBoxFor(x => Model.CustomRate, htmlAttributes: new { @id = "customrate", @class = "form-control", style = "width:100px;" })
      @Html.ValidationMessageFor(x => Model.CustomRate, "", new { @class = "text-danger" })
    </div>
  </div>

截图:

渲染的内容:

  div class="form-group">
  <div class="row">
    <input checked="checked" class="col-md-1" data-val="true" data-val-required="The PriceOption field is required." id="comed" name="PriceOption" type="radio" value="True" />
    <label for="Use_price_from_current_ComEd_rate">Use price from current rate plan"</label>
    <select class="form-control" data-val="true" data-val-required="Please select a rate plan" id="rateplans" name="SelectedRatePlan" required="Select Rate Plan" style="width:100px;"><option value="">select</option>
      <option value="B70">B70</option>
      <option value="B71">B71</option>
      <option value="B90">B90</option>
      <option value="B91">B91</option>
      <option value="H70">H70</option>
      <option value="H71">H71</option>
      <option value="H90">H90</option>
      <option value="H91">H91</option>
      <option value="R70">R70</option>
      <option value="R71">R71</option>
      <option selected="selected" value="R90">R90</option>
      <option value="R91">R91</option>
    </select>
    <span class="field-validation-valid text-danger" data-valmsg-for="SelectedRatePlan" data-valmsg-replace="true"></span>

  </div>
  </div>
  <div class="form-group">
  <div class="row">
    <input class="col-md-1" id="custom" name="PriceOption" type="radio" value="False" />
    <label for="Enter_custom_price">Enter custom price</label>
    <input class="form-control" data-val="true" data-val-maxlength="The field CustomRate must be a string or array type with a maximum length of &#x27;7&#x27;." data-val-maxlength-max="7" data-val-required="The CustomRate field is required." id="customrate" name="CustomRate" style="width:100px;" type="text" value="" />
    <span class="field-validation-valid text-danger" data-valmsg-for="CustomRate" data-valmsg-replace="true"></span>
  </div>
  </div>

我遇到了一些关于实现从 ValidationAttribute、IClientModelValidator 派生的自定义验证器的问题,或者是否有更简单的方法来实现我想要的?谢谢。

【问题讨论】:

    标签: c# jquery html css


    【解决方案1】:

    Expressive Annotations 是我在生产项目中多次使用的库。只需在视图模型上指定条件即可。

    所以你会有类似的东西:

    using ExpressiveAnnotations.Attributes;//reference at the top
    
    public class EnrollmentModel : BaseModel
    {
        ...
        public bool PriceOption { get; set; }
    
        [Required(ErrorMessage = "Please select a rate plan")]
        public string SelectedRatePlan { get; set; }
    
        public IEnumerable<SelectListItem> RatePlans { get; set; }
    
        [RequiredIf("PriceOption == true")]
        public string CustomRate { get; set; }
        ...
    }
    

    【讨论】:

    • 谢谢。我试过了,它适用于文本框。但是,它仍然要我为下拉菜单选择一些内容。
    猜你喜欢
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    • 2012-04-12
    • 2016-03-28
    • 2012-02-07
    相关资源
    最近更新 更多