繁体   English   中英

Html.DropDownList对于每个选定值的不同功能

[英]Html.DropDownListFor different function for each value selected

因此,在我的ASP.NET MVC应用程序中,我有一个带有多个值的下拉列表的视图。 基本上,我希望能够为每个选定的值调用不同的JS函数。

模型:

[Display(Name = "Rate")]
public string Rate { get; set; }

这是我的看法:

List<SelectListItem> rate = new List<SelectListItem>();
    rate.Add(new SelectListItem
    {
        Text = "1 Rate",
        Value = "1",

    });

    rate.Add(new SelectListItem
    {
        Text = "2 Rates",
        Value = "2"
    });
    rate.Add(new SelectListItem
    {
        Text = "3 Rates",
        Value = "3",
    });
    rate.Add(new SelectListItem
    {
        Text = "4 Rates",
        Value = "4"
    });
    rate.Add(new SelectListItem
    {
        Text = "5 Rates",
        Value = "5"
    });

}
    <div class="form-group">
          @Html.LabelFor(model => model.Rate, htmlAttributes: new { @class = "control-label text-left " })
    <div class="">

            @Html.DropDownListFor(model => rate,
                new SelectList(rate, "Text", "Value"),
                "Vælg rate",
                new { @class = "form-control", required = "required" })

    </div>
        </div>

我不确定确切在哪里添加我的js函数。 我尝试添加“ onchange”或“ onclick”之类的东西,但是它们不起作用。

 @Html.DropDownListFor(x => x.subitemid, Enumerable.Empty<SelectListItem>(), "Select Sub Category", new { @class = "form-control select2" })



     <script type="text/javascript">
    $(document).ready(function () {
        $('select#subitemid').change(function () {
            var itemId = $(this).val();
});
});
</script>

我认为这对您有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM