Javascript mvc 5如果dropdownlist选择的选项与所需的不同型号属性
我有一个包含多个控件的视图。我想要的是,当我的DropDownList中有一个特定值被选中时,如果该值被选中,那么我希望需要一个不同的控件属性。通常不需要此属性。如果在我的dropdownlist中选择了此值,我只希望它成为一项要求。在我的模型中,我通常只需将required添加到属性中,但如何才能像上面所问的那样做到这一点。如果“usuage=value1”,则数量+所需单位。在控制器操作中是否有这样做的方法?或者只能通过JavaScript实现。这是我在这种情况下的代码Javascript mvc 5如果dropdownlist选择的选项与所需的不同型号属性,javascript,c#,mvvm,asp.net-mvc-5,Javascript,C#,Mvvm,Asp.net Mvc 5,我有一个包含多个控件的视图。我想要的是,当我的DropDownList中有一个特定值被选中时,如果该值被选中,那么我希望需要一个不同的控件属性。通常不需要此属性。如果在我的dropdownlist中选择了此值,我只希望它成为一项要求。在我的模型中,我通常只需将required添加到属性中,但如何才能像上面所问的那样做到这一点。如果“usuage=value1”,则数量+所需单位。在控制器操作中是否有这样做的方法?或者只能通过JavaScript实现。这是我在这种情况下的代码 <div cl
<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>
@LabelFor(x=>x.Usage,
htmlAttributes:new{@class=“control label”})
@Html.DropDownListFor(x=>x.Usage,
(IEnumerable)ViewBag.UsageDDL,
“”,新的{@class=“form control”})
@Html.ValidationMessageFor(x=>x.Usage,“,
新建{@class=“text danger”})
@LabelFor(x=>x.Quantity,
htmlAttributes:new{@class=“control label”})
@EditorFor(x=>x.Quantity,新的{htmlAttributes=
新的{@class=“form control”,@type=“number”})
@Html.ValidationMessageFor(x=>x.Quantity,“,
新建{@class=“text danger”})
@LabelFor(x=>x.Units,
htmlAttributes:new{@class=“control label”})
@Html.DropDownListFor(x=>x.Units,
(IEnumerable)ViewBag.unitsdl,
“”,新的{@class=“form control”})
@Html.ValidationMessageFor(x=>x.Units,“,
新建{@class=“text danger”})
似乎您确实需要使用客户端代码(显然是javascript),尽管您可能完全可以在服务器端完成,但它正在穿越最浅的流
你可以用这样的东西:
@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>
@Html.DropDownList(“用法”,新建SelectListItem[]
{
新建SelectListItem(){Text=“one”,Value=“0”},
新建SelectListItem(){Text=“two”,Value=“1”},
新的{@onchange=“handler(this.value)”
});
函数处理程序(val){
//选项1:根据使用情况刷新当前页面,并在剃须刀页面中对数量和单位进行除盐。
window.location.href=“/Controller/CurrentAction?Usage=“+val;
//选项2:在不刷新页面的情况下,使用javascrip代码对数量和单位进行除盐。
var form=document.forms[0];
form.querySelector('input[name=“Quantity”]”)。disabled=true;
form.querySelector('input[name=“Unit”]”)。disabled=true;
}
我希望这将对您有所帮助似乎您确实需要使用客户端代码(显然是javascript),尽管您可能完全可以在服务器端完成,但它正在穿越最浅的领域 你可以用这样的东西:
@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>
@Html.DropDownList(“用法”,新建SelectListItem[]
{
新建SelectListItem(){Text=“one”,Value=“0”},
新建SelectListItem(){Text=“two”,Value=“1”},
新的{@onchange=“handler(this.value)”
});
函数处理程序(val){
//选项1:根据使用情况刷新当前页面,并在剃须刀页面中对数量和单位进行除盐。
window.location.href=“/Controller/CurrentAction?Usage=“+val;
//选项2:在不刷新页面的情况下,使用javascrip代码对数量和单位进行除盐。
var form=document.forms[0];
form.querySelector('input[name=“Quantity”]”)。disabled=true;
form.querySelector('input[name=“Unit”]”)。disabled=true;
}
我希望这将帮助您模型不能动态更改,因此您可能希望创建自己的验证方法。(客户端和服务器端)模型不能动态更改,因此您可能需要创建自己的验证方法。(客户端和服务器端。)