具有键 'Survey_Group' 的 ViewData 项是 'System.String' 类型,但必须是 'IEnumerable<SelectListItem>' 类型
The ViewData item that has the key 'Survey_Group' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'
我已经将数据库中的值绑定到下拉列表。
但是当我尝试在视图中提交时,它显示:
The ViewData item that has the key 'Survey_Group' is of type
'System.String' but must be of type 'IEnumerable'.
谁能指导一下?谢谢。
型号:
[MaxLength(100)]
[DisplayName("Owner Groups")]
public string Survey_Group { get; set; }
查看:
@Html.LabelFor(model => model.Survey_Group, "Owner Groups")
@Html.DropDownListFor(model => model.Survey_Group, (SelectList)ViewBag.GroupList)
控制器:
public ActionResult SURV_Main_Create()
{
ViewBag.CurrentPage = "create";
var model = new SURV_Main_Model();
ViewBag.GroupList = new SelectList(db.SURV_Group_Model, "Group_ID", "GroupName");
return View(model);
}
// POST: /SURV_Main/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SURV_Main_Create(SURV_Main_Model surv_main_model)
{
if (ModelState.IsValid)
{
var r = new Random();
int randomno = r.Next(1, 1000000);
surv_main_model.Survey_Key = "SURV" + DateTime.Now.ToString("yyyyMMddHHmmss") + randomno;
surv_main_model.Survey_Creator = User.Identity.Name;
db.SURV_Main_Model.Add(surv_main_model);
db.SaveChanges();
return RedirectToAction("SURV_Main_Edit", new { id = surv_main_model.Survey_ID });
}
return View(surv_main_model);
}
在您的 POST 方法中,当模型无效时,您 return 视图,但没有为 ViewBag.GroupList
赋值(您在 DropDownListFor()
方法)所以它是空的,因此是例外。
在你需要的POST方法中
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SURV_Main_Create(SURV_Main_Model surv_main_model)
{
if (ModelState.IsValid)
{
....
}
ViewBag.GroupList = new SelectList(db.SURV_Group_Model, "Group_ID", "GroupName"); // add this
return View(surv_main_model);
}
我已经将数据库中的值绑定到下拉列表。 但是当我尝试在视图中提交时,它显示:
The ViewData item that has the key 'Survey_Group' is of type 'System.String' but must be of type 'IEnumerable'.
谁能指导一下?谢谢。
型号:
[MaxLength(100)]
[DisplayName("Owner Groups")]
public string Survey_Group { get; set; }
查看:
@Html.LabelFor(model => model.Survey_Group, "Owner Groups")
@Html.DropDownListFor(model => model.Survey_Group, (SelectList)ViewBag.GroupList)
控制器:
public ActionResult SURV_Main_Create()
{
ViewBag.CurrentPage = "create";
var model = new SURV_Main_Model();
ViewBag.GroupList = new SelectList(db.SURV_Group_Model, "Group_ID", "GroupName");
return View(model);
}
// POST: /SURV_Main/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SURV_Main_Create(SURV_Main_Model surv_main_model)
{
if (ModelState.IsValid)
{
var r = new Random();
int randomno = r.Next(1, 1000000);
surv_main_model.Survey_Key = "SURV" + DateTime.Now.ToString("yyyyMMddHHmmss") + randomno;
surv_main_model.Survey_Creator = User.Identity.Name;
db.SURV_Main_Model.Add(surv_main_model);
db.SaveChanges();
return RedirectToAction("SURV_Main_Edit", new { id = surv_main_model.Survey_ID });
}
return View(surv_main_model);
}
在您的 POST 方法中,当模型无效时,您 return 视图,但没有为 ViewBag.GroupList
赋值(您在 DropDownListFor()
方法)所以它是空的,因此是例外。
在你需要的POST方法中
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SURV_Main_Create(SURV_Main_Model surv_main_model)
{
if (ModelState.IsValid)
{
....
}
ViewBag.GroupList = new SelectList(db.SURV_Group_Model, "Group_ID", "GroupName"); // add this
return View(surv_main_model);
}