我想在控制器中获取所选的下拉列表值。
它出现以下错误
没有类型为'IEnumerable< SelectListItem>'的ViewData项它有关键'studentid'
控制器代码
[HttpGet]
public ActionResult CallSqlScalarFuction()
{
IList<int> StudentIDs = db.Students.Select(x => x.ID).ToList();
ViewBag.studentid = StudentIDs.Select(x => new SelectListItem()
{
Text = x.ToString(),
Value = x.ToString()
});
return View();
}
[HttpPost]
public ActionResult CallSqlScalarFuction(int studentid)
{
string sql = @"Select dbo.GetStudentNameById(@p0) as FullName";
var studentFullName = db.Database.SqlQuery<string>(sql, studentid).ToList();
ViewBag.studentFullName = studentFullName[0].ToString();
return View();
}
CallSqlScalarFuction.cshtml
@using (Html.BeginForm("CallSqlScalarFuction", "Students",FormMethod.Post,null))
{
@Html.Label("Please select StudentID:")
@Html.DropDownList("studentid", (IEnumerable<SelectListItem>)ViewBag.studentid)
<input type="submit" value="submit" class="btn btn-primary" />
}