jQuery MVC4 Razor 中的下拉列表 SelectedItemChanged 事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19450816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:00:02  来源:igfitidea点击:

Dropdownlist SelectedItemChanged Event in MVC4 Razor?

jqueryasp.net-mvcasp.net-mvc-4razor

提问by Hyman

Is there any event available for Dropdownlist like onChange, etc. in MVC4? Do we have to use Javascript/jQuery in order to make some manipulations like DropwowlistSelectedItemChanged, etc? Or there is a way only using the Razor or MVC4 features?

MVC4 中是否有任何可用于 Dropdownlist 的事件,例如 onChange 等?我们是否必须使用 Javascript/jQuery 才能进行一些操作,例如 DropwowlistSelectedItemChanged 等?或者有一种方法只使用 Razor 或 MVC4 功能?

On the other hand, could you please give me some examples how can I retrieve data and change a label's text with this data according to a Dropdownlist Selected Value? For example when selecting a City from Dropdownlist, I want to get an address data from database by using the selected city id and then show retrieved address on a label. I would be appreciated if you clarify me about the issue above and give me an example to achieve this.

另一方面,您能否给我一些示例,如何根据下拉列表选定值检索数据并使用此数据更改标签文本?例如,当从下拉列表中选择一个城市时,我想通过使用选定的城市 id 从数据库中获取地址数据,然后在标签上显示检索到的地址。如果您向我澄清上述问题并给我一个例子来实现这一点,我将不胜感激。

Controller:

控制器:

private void PopulateMeetingsDropDownList(object selectedMeetings = null)
    {
        var meetingsQuery = repository.Meetings
            .Join(repository.Cities, m => m.MeetingCityId, c => c.CityID,
                (m, c) => new
                {
                    CityID = c.CityID,
                    CityName = c.CityName,
                    MeetingDate = m.MeetingStartDate
                }
            )
            .OrderBy(x => x.CityID)
            .AsEnumerable()
            .Select(
                i => new
                {
                    CityID = i.CityID,
                    DisplayValue = string.Format(
                        "{0} ({1:dd MMMM yyyy})",
                        i.CityName, i.MeetingDate)
                }
            ).ToList();
        ViewData["MeetingId"] = new SelectList(meetingsQuery, "CityID", "DisplayValue", selectedMeetings);
    }

View:

看法:

<label>Meeting</label>
@Html.DropDownListFor(m => m.MeetingId, ViewData["MeetingId"] as SelectList,"---- Select ----", new { name = "meetingId", id = "meetingId"}) 

回答by AthibaN

Try this,

尝试这个,

@Html.DropDownListFor(model => model.MyProp, (SelectList)ViewBag.ListItems, new { @id = "MyId", onchange = "MyFunction()" })

<script type="text/javascript">
    function MyFunction() {
        alert('Changed');
        $('#YourLabelId').val('ReplaceWithThisValue');
    }
</script>

or this

或这个

@Html.DropDownListFor(model => model.MyProp, (SelectList)ViewBag.ListItems, new { @id = "MyId"})

<script type="text/javascript">
   $('#MyId').change(function(){
        alert('Changed');
        $('#YourLabelId').val('ReplaceWithThisValue');
    });
</script>

回答by Raphael

You have to use jquery here you have a simple example

你必须在这里使用 jquery 你有一个简单的例子

<select id="myCombo" name="myCombo">
   <option>a</option>
   <option>b</option>
   <option>c</option>
</select>
<script type="text/javascript">
 $('#myCombo').change(function(){
   //Here goes your code 
 });
</script>

回答by keivan kashani

You must use onchange property in a new {} side of declaring class property like this example

您必须像此示例一样在声明类属性的新 {} 端使用 onchange 属性

[@Html.DropDownListFor(model => model.IpoId, new SelectList(Model.IpoList, "Value", "Text", Model.IpoId), "--?????? ????--", new {@class = "chzn-select chzn-rtl", @onchange = "IpoFilterOnChange();" })]

[@Html.DropDownListFor(model => model.IpoId, new SelectList(Model.IpoList, "Value", "Text", Model.IpoId), "--?????? ????--", new {@class = "chzn-select chzn-rtl", @onchange = "IpoFilterOnChange();" })]

回答by keivan kashani

You must use onchange property in a new {} side of declaring class property like this example

您必须像此示例一样在声明类属性的新 {} 端使用 onchange 属性

@Html.DropDownListFor(model => model.IpoId, new SelectList(Model.IpoList, "Value", "Text", Model.IpoId), "--?????? ????--", new {@class = "chzn-select chzn-rtl", @onchange = "IpoFilterOnChange();" })

回答by keivan kashani

You must use onchange property in a new {} side of declaring class property like this example

您必须像此示例一样在声明类属性的新 {} 端使用 onchange 属性

@Html.DropDownListFor(model => model.IpoId, new SelectList(Model.IpoList, "Value", "Text", Model.IpoId), "--?????? ????--", new {@class = "chzn-select chzn-rtl", @onchange = "IpoFilterOnChange();" })]

@Html.DropDownListFor(model => model.IpoId, new SelectList(Model.IpoList, "Value", "Text", Model.IpoId), "--?????? ????--", new {@class = "chzn-select chzn-rtl", @onchange = "IpoFilterOnChange();" })]