无法从下拉列表中向数据库添加值

时间:2021-11-21 14:13:01

I have a cascading dropdownlist ,using razor view syntax. Both are populated from database and the 2nd dropdownlist is populated with ajax method.

我有一个级联下拉列表,使用razor视图语法。两者都是从数据库填充的,第二个下拉列表是用ajax方法填充的。

1.  @Html.DropDownList("LevelMaster", (IEnumerable<SelectListItem>)ViewData["LevelMaster"], "Select Master")

2.  <select id="BranchID" name="Branch"></select>

When i am trying to insert values into database using the these two dropdown selected value. Getting an error :

当我尝试使用这两个下拉选择值将值插入数据库时​​。得到错误:

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'LevelMaster'.

 <span>
  @Html.DropDownList("LevelMaster", (IEnumerable<SelectListItem>)ViewData["LevelMaster"], "Select Master")
 </span>

Need some help.

需要一些帮助。

2 个解决方案

#1


1  

I think that the problem is because you have ViewData["LevelMaster"] and at the same time LevelMaster is the name of your dropDownList. Try changing the name in ViewData something like ViewData["LevelMasterViewData"]

我认为问题是因为你有ViewData [“LevelMaster”],同时LevelMaster是你的dropDownList的名字。尝试更改ViewData中的名称,如ViewData [“LevelMasterViewData”]

#2


1  

For DropDownList you need to provide which item you have to display as well as which will be the displayed for complex type.

对于DropDownList,您需要提供必须显示的项目以及复杂类型的显示项目。

 @Html.DropDownList("LevelMaster", new SelectList(ViewData["LevelMaster"], "Level", "Price"), "--Default--")

Better option would be using a DropDownListFor with model property assigned to collect the value.

更好的选择是使用DropDownListFor并指定模型属性来收集值。

@Html.DropDownListFor(m=>m.LevelMaster, new SelectList(ViewData["LevelMaster"], "Level", "Price"), "--Default--")

Where LevelMaster is of type string.

LevelMaster的类型为string。

#1


1  

I think that the problem is because you have ViewData["LevelMaster"] and at the same time LevelMaster is the name of your dropDownList. Try changing the name in ViewData something like ViewData["LevelMasterViewData"]

我认为问题是因为你有ViewData [“LevelMaster”],同时LevelMaster是你的dropDownList的名字。尝试更改ViewData中的名称,如ViewData [“LevelMasterViewData”]

#2


1  

For DropDownList you need to provide which item you have to display as well as which will be the displayed for complex type.

对于DropDownList,您需要提供必须显示的项目以及复杂类型的显示项目。

 @Html.DropDownList("LevelMaster", new SelectList(ViewData["LevelMaster"], "Level", "Price"), "--Default--")

Better option would be using a DropDownListFor with model property assigned to collect the value.

更好的选择是使用DropDownListFor并指定模型属性来收集值。

@Html.DropDownListFor(m=>m.LevelMaster, new SelectList(ViewData["LevelMaster"], "Level", "Price"), "--Default--")

Where LevelMaster is of type string.

LevelMaster的类型为string。