为了账号安全,请及时绑定邮箱和手机立即绑定

如何关闭下拉列表所需的消息?

如何关闭下拉列表所需的消息?

C#
狐的传说 2022-11-13 13:56:37
我有一个包含两个单选按钮的表单。一个与下拉菜单相关联,另一个与文本框相关联。如果用户选择第二个单选按钮(文本框),我想关闭下拉菜单所需的消息,因为当用户按下“提交”按钮时,下拉菜单会显示一条消息“请选择一个列表中的项目。”顺便说一句,如果我在选择第二个单选按钮时禁用下拉菜单,我可以让它正常工作,但我想知道是否有另一种方法(可能更好),因为当你禁用下拉菜单时,它会变灰。截图:模型: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>
查看完整描述

1 回答

?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

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

 public bool? PriceOption { get; set; }

该问号表示此属性可以为空。

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

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

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


查看完整回答
反对 回复 2022-11-13
  • 1 回答
  • 0 关注
  • 220 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号