c# - ASP.Net 如何在提交时删除/忽略特定元素的验证?

标签 c# jquery html css

我有一个 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>

截图:

enter image description here

渲染的内容:

  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 派生的自定义验证器的事情,或者是否有更简单的方法来实现我想要的?谢谢。

最佳答案

Expressive Annotations是我在生产元素中多次使用的库。只需在 View 模型上指定条件即可。

所以你会有这样的东西:

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; }
    ...
}

关于c# - ASP.Net 如何在提交时删除/忽略特定元素的验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55302030/

相关文章:

javascript - Jquery Datepicker,数字字段中的年份增量

html - 按钮导致输入字段在悬停时向上移动(在 chrome 上)

html - 没有javascript的IE6上的响应式网页布局

c# - UWP 获取文本 block 中的隐藏文本

c# - 错误 : "The node to be inserted is from a different document context"

c# - 在 fileupload 控件中上传之前识别文件是否打开

c# - MVVM + Reactive + WCF 回调

jquery - 水平和垂直中心模态 Div IE 问题

javascript - 如何像媒体查询一样在断点处禁用 JavaScript

html - 高度为 :auto and overflow doesn't work 的 IE11 垂直 flex 盒布局