【问题标题】:How to turn off the required message for drop-down list?如何关闭下拉列表所需的消息?
【发布时间】:2019-03-25 18:07:27
【问题描述】:

我有一个包含两个单选按钮的表单。一个与下拉菜单相关联,另一个与文本框相关联。如果用户选择第二个单选按钮(文本框),我想关闭下拉所需的消息,因为当用户按下“提交”按钮时,下拉菜单会显示一条消息“请选择一个列表中的项目。”

顺便说一句,如果我在选择第二个单选按钮时禁用下拉菜单,我就能正常工作,但我想知道是否有其他方法(也许更好),因为当你禁用下拉菜单时,它会改变它变灰。

截图:

型号:

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

  [RequiredIf("PriceOption == true")]
  public string SelectedRatePlan { get; set; }

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

  [RequiredIf("PriceOption == false")]
  public string CustomRate { get; set; }
  ...
}

查看:

@using (Html.BeginForm())
{
  @Html.AntiForgeryToken()

  <fieldset>
    <div class="form-group">
      <div class="row">
        @Html.Label("Price Option:")
      </div>
    </div>
    <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 plan")
        &nbsp;
        &nbsp;
        &nbsp;
        @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")
        &nbsp;
        &nbsp;
        &nbsp;
        @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">
        <div class="col-md-8">
          <input type="button" value="Cancel" class="btn btn-link" onclick="location.href = '@Url.Action("Index", "Home")';" />
          &nbsp;
          <button type="submit" value="Save" class="btn btn-primary">Submit</button>
        </div>
      </div>
    </div>
  </fieldset>
}

【问题讨论】:

    标签: c# jquery html asp.net-mvc asp.net-core


    【解决方案1】:

    您需要在模型中指定该字段是可选的,因为 bool 不能为空。 添加 ”?”在“bool”之后或使用“Nullable”而不是“bool”

     public bool? PriceOption { get; set; }
    

    那个问号表示这个属性可以为空。

    在此页面中,您可以找到如何在单选按钮上实现更改事件: https://devdojo.com/tutorials/how-to-detect-radio-box-change-with-jquery

    您可以使用以下代码删除属性:

    $("#selectElementId").removeAttr("required");
    

    【讨论】:

    • 我进行了建议的更改,但没有奏效。我仍然收到“请选择...”
    • 您还需要从“select”元素中删除“required”属性。
    • 在单选按钮的更改事件中,您可以删除或添加您想要的任何属性。
    • 只需从“select”中删除“required”属性就可以了,它就可以工作了——bool?似乎没有什么不同。
    • 在服务器端,您至少应该得到一个验证错误,因为“PriceOption”没有数据。如果您使用默认的 MVC 验证,它将阻止您提交表单。还有一个逻辑问题,当你没有设置 bool 属性时,它默认为“false”,这是错误的。
    猜你喜欢
    • 2014-09-20
    • 1970-01-01
    • 2019-01-05
    • 2013-03-08
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多