mvc 5 如果下拉列表选择的选项比所需的不同模型 属性
mvc 5 if dropdownlist selected option than required different model property
我有一个包含多个控件的视图。我想要的是,当我的下拉列表中的 1 个选择了特定值时,如果选择了该值,那么我需要一个不同的控件 属性。通常不需要这个 属性。如果在我的下拉列表中选择了这个值,我只希望它成为一个要求。在我的模型中,我通常只需要将 属性 添加到 属性 即可,但我该如何按照上面的要求执行此操作。如果“usuage=value1”,则需要数量 + 单位。有没有办法在控制器动作中做到这一点?或者只能通过 JavaScript。这是我针对这种情况的代码。
<div class="form-group row">
<div class="col-md-4 col-lg-4">
@Html.LabelFor(x => x.Usage,
htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-8 col-lg-8">
@Html.DropDownListFor(x => x.Usage,
(IEnumerable<SelectListItem>) ViewBag.UsageDDL,
"", new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Usage, "",
new { @class = "text-danger" })
</div>
</div> <div class="form-group row">
<div class="col-md-4 col-lg-4">
@Html.LabelFor(x => x.Quantity,
htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-8 col-lg-8">
@Html.EditorFor(x => x.Quantity, new { htmlAttributes =
new { @class = "form-control", @type = "number" }})
@Html.ValidationMessageFor(x => x.Quantity, "",
new { @class = "text-danger" })
</div>
</div>
<div class="form-group row">
<div class="col-md-4 col-lg-4">
@Html.LabelFor(x => x.Units,
htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-8 col-lg-8">
@Html.DropDownListFor(x => x.Units,
(IEnumerable<SelectListItem>) ViewBag.UnitsDDL,
"", new { @class="form-control" })
@Html.ValidationMessageFor(x => x.Units, "",
new { @class = "text-danger" })
</div>
</div>
看来您肯定需要使用 client-side 代码(显然 javascript),尽管您可以完全做到 server-side 但它在最浅的地方穿过溪流。
你可以使用这样的东西:
@Html.DropDownList("Usage", new SelectListItem[]
{
new SelectListItem() { Text = "one", Value = "0" },
new SelectListItem() { Text = "two", Value = "1" }},
new { @onchange="handler(this.value)"
});
<script>
function handler(val){
//option1 : refresh current page based on Usage and make Quantity and Unit desabled in your Razor Page.
window.location.href = "/Controller/CurrentAction?Usage=" + val;
// option2 : make Quantity and Unit desabled by javascrip code without refresh page.
var form = document.forms[0];
form.querySelector('input[name="Quantity"]').disabled = true;
form.querySelector('input[name="Unit"]').disabled = true;
}
</script>
希望对您有所帮助
我有一个包含多个控件的视图。我想要的是,当我的下拉列表中的 1 个选择了特定值时,如果选择了该值,那么我需要一个不同的控件 属性。通常不需要这个 属性。如果在我的下拉列表中选择了这个值,我只希望它成为一个要求。在我的模型中,我通常只需要将 属性 添加到 属性 即可,但我该如何按照上面的要求执行此操作。如果“usuage=value1”,则需要数量 + 单位。有没有办法在控制器动作中做到这一点?或者只能通过 JavaScript。这是我针对这种情况的代码。
<div class="form-group row">
<div class="col-md-4 col-lg-4">
@Html.LabelFor(x => x.Usage,
htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-8 col-lg-8">
@Html.DropDownListFor(x => x.Usage,
(IEnumerable<SelectListItem>) ViewBag.UsageDDL,
"", new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Usage, "",
new { @class = "text-danger" })
</div>
</div> <div class="form-group row">
<div class="col-md-4 col-lg-4">
@Html.LabelFor(x => x.Quantity,
htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-8 col-lg-8">
@Html.EditorFor(x => x.Quantity, new { htmlAttributes =
new { @class = "form-control", @type = "number" }})
@Html.ValidationMessageFor(x => x.Quantity, "",
new { @class = "text-danger" })
</div>
</div>
<div class="form-group row">
<div class="col-md-4 col-lg-4">
@Html.LabelFor(x => x.Units,
htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-8 col-lg-8">
@Html.DropDownListFor(x => x.Units,
(IEnumerable<SelectListItem>) ViewBag.UnitsDDL,
"", new { @class="form-control" })
@Html.ValidationMessageFor(x => x.Units, "",
new { @class = "text-danger" })
</div>
</div>
看来您肯定需要使用 client-side 代码(显然 javascript),尽管您可以完全做到 server-side 但它在最浅的地方穿过溪流。
你可以使用这样的东西:
@Html.DropDownList("Usage", new SelectListItem[]
{
new SelectListItem() { Text = "one", Value = "0" },
new SelectListItem() { Text = "two", Value = "1" }},
new { @onchange="handler(this.value)"
});
<script>
function handler(val){
//option1 : refresh current page based on Usage and make Quantity and Unit desabled in your Razor Page.
window.location.href = "/Controller/CurrentAction?Usage=" + val;
// option2 : make Quantity and Unit desabled by javascrip code without refresh page.
var form = document.forms[0];
form.querySelector('input[name="Quantity"]').disabled = true;
form.querySelector('input[name="Unit"]').disabled = true;
}
</script>
希望对您有所帮助