我有两个下拉列表来过滤purpose.How,我能把这个下拉列表更改为追赶下拉列表吗?
public ActionResult Index()
{
REFINED_DBEntities db = new REFINED_DBEntities();
ViewBag.Subdivision =
new SelectList(db.Retention_Model_Predictions_DS_Manual.Select(m => m.Subdivision).Distinct().OrderBy(c=>c.ToUpper()),
"Subdivision");
ViewBag.UnderwriterName =
new SelectList(db.Retention_Model_Predictions_DS_Manual.Select(m => m.Underwriter_Name).Distinct(),
"Underwriter_Name");
return View();
}HTML视图
<div class="form-group ">
@Html.DropDownList("Subdivision", (IEnumerable<SelectListItem>)ViewBag.Subdivision, "Select Region", new { @class = "form-control", @id = "subDivision" })
</div>
<div class="form-group ">
@Html.DropDownList("Underwriter_Name", (IEnumerable<SelectListItem>)ViewBag.UnderwriterName, "Select Underwriter", new { @class = "form-control", @id = "uwriter" })
</div>发布于 2017-10-26 14:32:11
级联下拉的思想是,在第一个下拉列表中选择一个值,它会触发一些操作来加载第二个下拉列表的选项。一个典型的例子是国家的下降和州的下降。每次用户选择一个国家时,国家下拉列表应该与该国家下的州一起更新。
-我将给您一个非常通用的国家-国家用例的例子。您可以使用相同的概念来构建特定的用例。
首先,为视图创建一个视图模型。创建一个类型为List<SelectListItem>的属性,以传递生成SELECT元素所需的选项列表和存储所选选项值的另一个属性。我们将为两个SELECT元素执行此操作。
public class CreateUserVm
{
public List<SelectListItem> Countries { set;get;}
public int SelectedCountryId { set;get;}
public List<SelectListItem> States { set;get;}
public int SelectedStateId { set;get;}
public CreateUserVm()
{
this.Countries = new List<SelectListItem>();
this.States = new List<SelectListItem>();
}
}现在,在GET操作方法中,创建该视图模型的一个对象,初始化第一个下拉列表的选项,在本例中是Countries属性,并将其发送到视图。
public ActionResult Create()
{
var vm=new CreateUserVm();
vm.Countries = GetCountries();
return View(vm);
}
private List<SelectListItem> GetCountries()
{
var list = new List<SelectListItem>
{
new SelectListItem() {Value = "1", Text = "USA"},
new SelectListItem() {Value = "2", Text = "India"},
};
return list;
}现在在您的视图中,它是强类型的,我们的视图模型。我们将使用DropDownListFor助手方法来呈现下拉式。
@model CreateUserVm
@using (Html.BeginForm("Index", "Home"))
{
@Html.DropDownListFor(a=>a.SelectedCountryId,Model.Countries,"Select one")
@Html.DropDownListFor(a => a.SelectedStateId, Model.States, "Select one",
new { data_url = Url.Action("GetStates") })
<input type="Submit" />
}这将呈现两个下拉列表,一个是带有Country选项的,另一个是空的(因为我们没有向States属性加载任何内容)。现在我们将有一些javascript(为了便于DOM操作,我们在这里使用jquery ),它将侦听第一个下拉( country )的change事件,读取所选的值,并对GetStates方法进行ajax调用,并传递所选的country选项值。
您可以看到,我为第二个下拉列表设置了一个html5数据属性,在那里我将url存储到GetStates方法。因此,在我的javascript中,我可以简单地读取这个数据属性值,并调用那个url来获取数据。
$(function () {
$("#SelectedCountryId").change(function () {
var v = $(this).val();
var url = $("#SelectedStateId").data("url") + '?countryId=' + v;
var $states = $("#SelectedStateId");
$.getJSON(url, function (states) {
$states.empty();
$.each(states, function (i, item) {
$states.append($("<option>").text(item.Text).val(item.Value));
});
});
});
});现在,让我们添加一个GetStates操作方法,它接受countryId,并以JSON数组的形式在SelectListItem列表中返回该国的状态。
public ActionResult GetStates(int countryId)
{
var states = new List<SelectListItem>();
if (countryId == 1)
{
states.Add(new SelectListItem() {Value = "101", Text = "Michigan"});
states.Add(new SelectListItem() { Value = "102", Text = "New York" });
}
else if (countryId == 2)
{
states.Add(new SelectListItem() { Value = "103", Text = "Kerala" });
states.Add(new SelectListItem() { Value = "104", Text = "Goa" });
}
return Json(states, JsonRequestBehavior.AllowGet);
}这里我对国家和州进行了硬编码。但是,如果您有一个包含这些数据的数据库,请用表中的数据替换硬编码的值。
在编辑记录时,所要做的就是根据保存的States在GET操作中加载CountryId属性。
https://stackoverflow.com/questions/46954959
复制相似问题