【发布时间】: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")
@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">
<div class="col-md-8">
<input type="button" value="Cancel" class="btn btn-link" onclick="location.href = '@Url.Action("Index", "Home")';" />
<button type="submit" value="Save" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</fieldset>
}
【问题讨论】:
标签: c# jquery html asp.net-mvc asp.net-core