尝试加载Drop,我在视图中收到此错误: 值不能为null或为空。 参数名称:名称
这是控制器
static List<AccountList> _acc = new List<AccountList>();
public ActionResult Index()
{
_acc = GetAccounts();
return View(_acc);
}
private List<AccountList> GetAccounts()
{
List<AccountList> temp = new List<AccountList>();
string sSource = "Test";
string sClinicId = "4";
string sStatus = null;
//This is simply retrieving the class from the web service where the list data is stored
ReconUIDataLayer.ClinicAccts retClinicAccts = new ReconUIDataLayer.ClinicAccts();
retClinicAccts = ReconUIDataLayer.UIDataLayer.LoadAccountInfo(sSource, sClinicId, ref sStatus);
temp = (from rows in retClinicAccts.ClinicAccts
select new AccountList
{
Text = rows.AcctName.ToString(),
Value = rows.AcctName.ToString(),
}).ToList();
return temp;
}
这是视图:
@model IEnumerable<C1MvcWebApplication3.Models.AccountList>
@using C1MvcWebApplication3.Models
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<table>
<tr><td>@Html.DropDownList("", new SelectList(Model.Select(x => new { Value = x, Text = x}),"AcctName","AcctName"))</td></tr>
</table>
</div>
</body>
</html>
帮助程序发生错误以创建下拉列表。
答案 0 :(得分:0)
我实际上做了两件事来解决这个问题: 1)刚刚将List移动到我的控制器
public ActionResult Index()
{
List<AccountList> _acc = new List<AccountList>();
_acc = GetAccounts();
return View(_acc);
}
我不知何故认为我不需要将List声明移动到ActionResult中,以后可能会因为不使它静止而咬我。
2)在我的视图中更改了呼叫
@Html.DropDownList("AccountList",new SelectList(Model, "Value", "Text"),"-Select")
答案 1 :(得分:0)
控制器代码
readonly DBDashboardEntities DB = new DBDashboardEntities();// DB Entity Object
public ActionResult Index()
{
//using viewdata
ViewData["SelectDropDown_List"] =new SelectList(DB.tblStudents.ToList(), "studID", "studName");
//using viewbag
ViewBag.SelectDropDownList = new SelectList(DB.tblStudents.ToList(), "studID", "studName");
return View();
}
查看页面代码:index.cshtml
<tr>
<td>
@Html.DropDownListFor(c => c.studName, new SelectList(
new List<Object>{
new { value = 0 , text = "Rose" },
new { value = 1 , text = "John" },
new { value = 2 , text = "Smith"}
},
"value",
"text", 2))
</td>
<td>
@Html.DropDownListFor(c => c.studID, ViewData["SelectDropDown_List"] as SelectList) @*ViewData requires type casting*@
</td>
<td>
@Html.DropDownList("SelectDropDownList") @*String must be same as ViewBag.SelectDropDownList*@
</td>
</tr>