gpt4 book ai didi

asp.net-mvc-3 - Mvc3 中的级联下拉菜单

转载 作者:行者123 更新时间:2023-12-04 18:19:10 26 4
gpt4 key购买 nike

我正在使用 Mvc3 我有 2 个下拉菜单,BankBranch 和城市。
在第一次加载 View 时,我在没有级联的情况下绑定(bind)了两个 dropdon。然后,如果用户选择城市,我想据此更改银行分行。
我很困惑如何同时实现这两件事。

提前致谢。

最佳答案

这篇博文应该能让你上路。它提供了普通表单帖子、microsoft ajax 表单帖子、jquery ajax 等的示例。

http://weblogs.asp.net/raduenuca/archive/2011/03/06/asp-net-mvc-cascading-dropdown-lists-tutorial-part-1-defining-the-problem-and-the-context.aspx

编辑:
通用代码说明


模型

public class CascadeModel {
public SelectList<City> Cities { get; set; }
public SelectList<BankBranch> BankBranches { get; set;}
public int CityId { get; set; }
public int BranchId { get; set; }
}

public class Branch {
public int Id { get; set;}
public string Name { get; set; }
}

Controller :
public ActionResult BranchSelector() {
var viewData = new CascadeModel();
viewData.Cities = new SelectList(Repository.GetAllCities(), "Id", "Name", selectedCity);
viewData.BankBranches = new SelectList(Repository.GetBranchesByCity(selectedCity), "Id", "Name", "");
return View(viewData);
}

public JsonResult GetBranches(int id) {
return Json(Repository.GetBranchesByCity(id), JsonRequestBehavior.AllowGet);
}

看法:
@model CascadeModel


@Html.DropDownListFor(m => m.CityId, Model.Cities, new { style = "width:250px" })
<br />
@Html.DropDownListFor(m => m.BranchId, Model.BankBranches, new { style = "width:250px" })

<script type="text/javascript">
$(document).ready(function() {
$("#CityId").bind('change', function() {
$.ajax({
url: '/Controller/GetBranches/' + $(this).val(),
success: function(data) {
//Clear the current branch ddl
//Load the new Branch data returned from the jquery call in the branches ddl
}
});
};
});
</script>

关于asp.net-mvc-3 - Mvc3 中的级联下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11044759/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com