C# 如何在.NET MVC中使用选定值在编辑视图中显示动态下拉列表?

C# 如何在.NET MVC中使用选定值在编辑视图中显示动态下拉列表?,c#,asp.net,asp.net-mvc,asp.net-mvc-4,dropdownlistfor,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Dropdownlistfor,我有一个表单,其中包括三个动态下拉列表。它们都是一对多的关系。第一个dropdownlist加载了值,另外两个在开始时为空。当用户从第一个下拉列表中选择一个值时,第二个下拉列表将加载相应的项目;当用户从第二个下拉列表中选择一个值时,第三个下拉列表将加载相关项目 用户可以创建表单并提交。我的CreateController GET和POST方法运行良好 现在我需要EditController来允许用户编辑他的表单。问题是如何用选定的值显示第二个和第三个动态下拉列表 在我的viewModel中:

我有一个表单,其中包括三个动态下拉列表。它们都是一对多的关系。第一个dropdownlist加载了值,另外两个在开始时为空。当用户从第一个下拉列表中选择一个值时,第二个下拉列表将加载相应的项目;当用户从第二个下拉列表中选择一个值时,第三个下拉列表将加载相关项目

用户可以创建表单并提交。我的CreateController GET和POST方法运行良好

现在我需要EditController来允许用户编辑他的表单。问题是如何用选定的值显示第二个和第三个动态下拉列表

在我的viewModel中:

    public Nullable<int> Tech_ID { get; set; }
    public string TechniqueName { get; set; }
    public SelectList Techniques { get; set; }

    public Nullable<int> BU_ID { get; set; }
    public string BuName { get; set; }
    public SelectList BusinessUnits { get; set; }
public可空技术标识{get;set;}
公共字符串技术名称{get;set;}
公共选择列表技术{get;set;}
公共可空buu ID{get;set;}
公共字符串BuName{get;set;}
public SelectList业务单元{get;set;}
在我的编辑控制器中:

ViewBag.Bulist = new SelectList(AllBus, "BU_ID", "BU_Title", form.BU_ID); 

var butechList = GetTechList(Convert.ToInt32(form.BU_ID));
IEnumerable<SelectListItem> butechs = butechList.Select(m => new SelectListItem()
{
    Value = m.Tech_ID.ToString(),
    Text = m.Technique.Tech_Title
});
ViewBag.Techlist = new SelectList(butechs, "Tech_ID", "Tech_Title", form.Tech_ID);
ViewBag.Bulist=新的选择列表(AllBus,“BU_ID”,“BU_Title”,form.BU_ID);
var butechList=GetTechList(转换为.ToInt32(form.BU_ID));
IEnumerable butechs=butechList.Select(m=>newselectListItem()
{
Value=m.Tech_ID.ToString(),
Text=m.technology.Tech\u标题
});
ViewBag.Techlist=新的选择列表(但是“Tech\u ID”、“Tech\u Title”、form.Tech\u ID);
在查看页面中:

@Html.DropDownListFor(model => model.BU_ID, 
    new SelectList((IEnumerable<SelectListItem>)@ViewBag.Bulist, "Value", "Text", Model.BU_ID), 
    new { id = "ddlBU" })

@Html.DropDownListFor(model => model.Tech_ID, 
    new SelectList((IEnumerable<SelectListItem>)@ViewBag.Techlist, "Value", "Text", Model.Tech_ID), 
    new { id = "ddlTechs" })
@Html.DropDownListFor(model=>model.BU_ID,
新的选择列表((IEnumerable)@ViewBag.Bulist,“值”,“文本”,Model.BU_ID),
新的{id=“ddlBU”})
@Html.DropDownListFor(model=>model.Tech_ID,
新建SelectList((IEnumerable)@ViewBag.Techlist,“值”,“文本”,Model.Tech\u ID),
新建{id=“ddlTechs”})

第一个下拉列表工作正常,但第二个不正常。

首先,您有一个包含
SelectList
属性的视图模型,因此请使用它,而不是使用
ViewBag
属性

接下来,删除
SelectList
构造函数中不必要的最后一个参数(
object selectedValue
)——您对模型中某个属性的绑定,以便忽略它(内部为
DropDwonListFor()
方法生成一个新的
IEnumerable
,并根据绑定到的属性值设置
selectedValue

但主要问题是,不必要地生成3
IEnumerable
。第一次使用

IEnumerable<SelectListItem> butechs = butechList.Select(...
但是这次它会抛出一个异常,因为
butechs
IEnumerable
并且
SelectListItem
不包含属性
Tech\u ID
Tech\u Title
(它需要是
new SelectList(butechs,“Value”,“Text”)
,以防止错误,但它没有意义,所以只需删除它)。然后尝试在
DropDownListFor()
方法内创建第三个
IEnumerable
(同样是它的无意义的额外头)

控制器中的代码只需

var butechList = GetTechList(form.BU_ID.Value); // its already an int!
model.Techniques = new SelectList(butechList, "Tech_ID", "Tech_Title");
return View(model);
在我看来

@Html.DropDownListFor(m => m.Tech_ID, Model.Techniques) // not sure why you would need to change the id from the default id="Tech_ID" to id="ddlBU"?

我还建议您研究代码,其中显示了生成级联dropdownlists的示例。特别是
private void ConfigureViewModel(ViewModel模型)
方法中的代码,它将根据所选的值生成正确的
SelectList
,并且可以从
Create()
Edit()
方法(如果需要返回视图,可以使用GET和POST)中调用它,您有一个包含
SelectList
属性的视图模型,因此请使用它,而不要使用
ViewBag
属性

接下来,删除
SelectList
构造函数中不必要的最后一个参数(
object selectedValue
)——您对模型中某个属性的绑定,以便忽略它(内部为
DropDwonListFor()
方法生成一个新的
IEnumerable
,并根据绑定到的属性值设置
selectedValue

但主要问题是,不必要地生成3
IEnumerable
。第一次使用

IEnumerable<SelectListItem> butechs = butechList.Select(...
但是这次它会抛出一个异常,因为
butechs
IEnumerable
并且
SelectListItem
不包含属性
Tech\u ID
Tech\u Title
(它需要是
new SelectList(butechs,“Value”,“Text”)
,以防止错误,但它没有意义,所以只需删除它)。然后尝试在
DropDownListFor()
方法内创建第三个
IEnumerable
(同样是它的无意义的额外头)

控制器中的代码只需

var butechList = GetTechList(form.BU_ID.Value); // its already an int!
model.Techniques = new SelectList(butechList, "Tech_ID", "Tech_Title");
return View(model);
在我看来

@Html.DropDownListFor(m => m.Tech_ID, Model.Techniques) // not sure why you would need to change the id from the default id="Tech_ID" to id="ddlBU"?

我还建议您研究代码,其中显示了生成级联dropdownlists的示例。特别是
private void ConfigureViewModel(ViewModel model)
方法中的代码,它将根据所选值生成正确的
SelectList
,可以从
Create()
Edit()
方法调用(如果需要返回视图,可以使用GET和POST)

您有什么问题?当您有一个视图模型时,为什么要使用
ViewBag
?当
ViewBag
属性已经是
SelectList
时,您为什么要在视图中创建第二个
SelectList
。您可能还需要查看中用于实现级联下拉列表的代码。非常感谢您的建议。我尝试在控制器中使用viewmodel而不是viewbag,并将视图页面更改为:`@Html.DropDownListFor(model=>model.Tech_ID,model.technics),new{ID=“ddlTechs”})`但仍然出现相同的错误:数据绑定:“System.Web.Mvc.SelectListItem”不包含名为“Tech\u ID”的属性。对不起,这是我的错