Jac*_*ack 1 asp.net-mvc jquery razor asp.net-mvc-4
在MVC4中是否有类似onChange等Dropdownlist的事件?我们是否必须使用Javascript/jQuery来进行DropwowlistSelectedItemChanged等操作?或者只有使用Razor或MVC4功能的方法?
另一方面,您能否告诉我一些示例如何根据下拉列表选择值检索数据并使用此数据更改标签的文本?例如,当从Dropdownlist中选择City时,我想通过使用所选的城市ID从数据库获取地址数据,然后在标签上显示检索到的地址.如果您向我澄清上述问题并给我一个实现此目的的示例,我将不胜感激.
控制器:
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);
}
Run Code Online (Sandbox Code Playgroud)
视图:
<label>Meeting</label>
@Html.DropDownListFor(m => m.MeetingId, ViewData["MeetingId"] as SelectList,"---- Select ----", new { name = "meetingId", id = "meetingId"})
Run Code Online (Sandbox Code Playgroud)
试试这个,
@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>
Run Code Online (Sandbox Code Playgroud)
或这个
@Html.DropDownListFor(model => model.MyProp, (SelectList)ViewBag.ListItems, new { @id = "MyId"})
<script type="text/javascript">
$('#MyId').change(function(){
alert('Changed');
$('#YourLabelId').val('ReplaceWithThisValue');
});
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30878 次 |
| 最近记录: |