MVC3中下拉列表的处理
我在asp.net mvc3处理中,有个问题当出现重得的输入时报The ViewData item that has the key 'Id' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'
我的代码是这样的。
#region 性别下拉列表选择
private void AddGenderToViewBag()
{
Dictionary<string, string> list = new Dictionary<string, string>();
list.Add("男", "男");
list.Add("女", "女");
SelectList items = new SelectList(list, "Value", "Key");
ViewBag.GenderList = items;
}
#endregion
public ActionResult insSngStu()
{
AddGenderToViewBag();
AddAddressToViewBag();
var stu = new Models.stu.student();
return View(stu);
}
[HttpPost]
public ActionResult insSngStu(Models.stu.student stu)
{
if (ModelState.IsValid)
{
int result = ro.InsertSngStu(stu);
if (result > 0)
{
return RedirectToAction("Index");
}
}
return View(stu);
}
页面代码:
<div class="editor-label">
@Html.LabelFor(model => model.Sex)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Sex, (SelectList)ViewBag.GenderList, "--请选择--", new { @class = "droplist" })
@Html.ValidationMessageFor(model => model.Sex)
</div>
当第一次添加时,不报错,但第二次有重复时,就报如上错误,不知如何处理?有没有人遇到过类型情况,你是如何解决的?