C# 如何在MVC中通过强类型视图中的存储过程绑定数据库中的下拉列表值。。?
我有一个强类型视图,如下所示:C# 如何在MVC中通过强类型视图中的存储过程绑定数据库中的下拉列表值。。?,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我有一个强类型视图,如下所示: <div class="form-group"> @Html.LabelFor(model => model.Text, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownListFor(model => model.Text
<div class="form-group">
@Html.LabelFor(model => model.Text, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Text, "Drop"); @*Error Line*@
@Html.ValidationMessageFor(model => model.Text, "", new { @class = "text-danger" })
</div>
基本上,我不想硬编码通过列表项查看的值,但希望通过存储过程将其从数据库绑定
因此,我有如下控制器
[HttpGet]
public ActionResult Dropdown()
{
BusDataContext dtnew=new BusDataContext();
ViewBag.Drop = new SelectList(dtnew.Bind_Dropdown().ToList(), "BusSrlNo", "Name");
return View();
}
public IEnumerable<DropdownValues> Bind_Dropdown()
{
var res = dt.Get_Bus_Dropdown().ToList();
List<DropdownValues> drop = new List<DropdownValues>();
for (var i = 0; i < res.Count; i++)
{
DropdownValues name = new DropdownValues();
name.drpbusid = Convert.ToInt32(res[i].BusSrlNo);
name.drpbusname = Convert.ToString(res[i].Name);
drop.Add(name);
}
return drop;
}
如上所述,还在ViewBag中存储下拉id和文本。
其中Bind_Dropdown()如下所示
[HttpGet]
public ActionResult Dropdown()
{
BusDataContext dtnew=new BusDataContext();
ViewBag.Drop = new SelectList(dtnew.Bind_Dropdown().ToList(), "BusSrlNo", "Name");
return View();
}
public IEnumerable<DropdownValues> Bind_Dropdown()
{
var res = dt.Get_Bus_Dropdown().ToList();
List<DropdownValues> drop = new List<DropdownValues>();
for (var i = 0; i < res.Count; i++)
{
DropdownValues name = new DropdownValues();
name.drpbusid = Convert.ToInt32(res[i].BusSrlNo);
name.drpbusname = Convert.ToString(res[i].Name);
drop.Add(name);
}
return drop;
}
存储过程将下拉值和文本返回为BusSrlNo和Name。
完成所有这些工作后,得到如下错误编译:
:
CS1928:“System.Web.Mvc.HtmlHelper”没有
包含“DropDownListFor”的定义和最佳扩展名
方法重载
'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper,
System.Linq.Expressions.Expression>,,
System.Collections.Generic.IEnumerable)'
有一些无效的参数
我不熟悉您试图使用的签名(可以从这样的字符串绑定值吗?) 如果将此切换到:
@Html.DropDownListFor(m => m.Text,
((IEnumerable<SelectListItem>)ViewBag.Drop.Items))
@Html.DropDownListFor(m=>m.Text,
((IEnumerable)ViewBag.Drop.Items))
1)更改此行:
ViewBag.Drop = new SelectList(dtnew.Bind_Dropdown().ToList(), "BusSrlNo", "Name");
@Html.DropDownListFor(model => model.Text, "Drop");
为此:
ViewBag.Drop = new SelectList(dtnew.Bind_Dropdown().ToList(), "drpbusid", "drpbusname");
@Html.DropDownList("anyName", ViewBag.Drop as SelectList)
2) 更改此行:
ViewBag.Drop = new SelectList(dtnew.Bind_Dropdown().ToList(), "BusSrlNo", "Name");
@Html.DropDownListFor(model => model.Text, "Drop");
为此:
ViewBag.Drop = new SelectList(dtnew.Bind_Dropdown().ToList(), "drpbusid", "drpbusname");
@Html.DropDownList("anyName", ViewBag.Drop as SelectList)
我已经复制了您的错误,这是我为使其正常工作所做的唯一两项更改。我希望它能对您有所帮助。仍然没有->无法将“System.Collections.Generic.List
1[MVC_DAL.DropdownValues]”类型的对象强制转换为“System.Collections.Generic.IEnumerable
1[System.Web.MVC.SelectListItem]”。