问题描述
我试图从填充使用LINQ-2-SQL映射数据库,采用ASP.NET MVC 2下拉列表,并保持收到此错误。
I am trying to populate a dropdown list from a database mapped with Linq-2-SQL, using ASP.NET MVC 2, and keep getting this error.
我很困惑,因为我声明类型的IEnumerable℃的变量; SelectListItem>
在第二行,但这个错误让我觉得这是不是这样的。我觉得这应该是很简单的,但我挣扎。任何帮助是AP preciated。
I am so confused because I am declaring a variable of type IEnumerable<SelectListItem>
on the second line, but the error makes me think this is not the case. I feel like this should be very simple, but I am struggling. Any help is appreciated.
下面是我的控制器的有趣的位:
Here are the interesting bits of my controller:
public ActionResult Create()
{
var db = new DB();
IEnumerable<SelectListItem> basetypes = db.Basetypes.Select(
b => new SelectListItem { Value = b.basetype, Text = b.basetype });
ViewData["basetype"] = basetypes;
return View();
}
和这里是我认为有趣的位:
And here are the interesting bits of my view:
<div class="editor-label">
<%: Html.LabelFor(model => model.basetype) %>
</div>
<div class="editor-field">
<%: Html.DropDownList("basetype") %>
<%: Html.ValidationMessageFor(model => model.basetype) %>
</div>
下面是提交表单时,POST操作
Here is the POST action when submitting the Form
// POST: /Meals/Create
[HttpPost]
public ActionResult Create(Meal meal)
{
if (ModelState.IsValid)
{
try
{
// TODO: Add insert logic here
var db = new DB();
db.Meals.InsertOnSubmit(meal);
db.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View(meal);
}
}
else
{
return View(meal);
}
}
感谢。
推荐答案
我有同样的问题,最后我得到了答案......
I had same problem, and finally I got the answer...
问题是,在POST操作,提交表单后,ModelState中是无效的,或者它在醒目的try / catch错误,因此返回查看。但此时的看法并没有在计算机[基本类型]
设置正确。
The problem is that in the POST action, after submitting the form, the ModelState is not valid, or it's catching an error in try/catch, so the View is returned. But this time the View has not the ViewData["basetype"]
correctly set.
您需要再次填充它,可能与之前相同的code,如此重复这样的:
You need to populate it again, probably with the same code used before, so repeat this:
var db = new DB();
IEnumerable<SelectListItem> basetypes = db.Basetypes.Select(
b => new SelectListItem { Value = b.basetype, Text = b.basetype });
ViewData["basetype"] = basetypes;
在在返回查看(粕)
的 [HttpPost]
方法。
正是这将解决你的问题:
exactly this will solve your problem:
[HttpPost]
public ActionResult Create(Meal meal)
{
if (ModelState.IsValid)
{
try
{
// TODO: Add insert logic here
var db = new DB();
db.Meals.InsertOnSubmit(meal);
db.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
var db = new DB();
IEnumerable<SelectListItem> basetypes = db.Basetypes.Select(
b => new SelectListItem { Value = b.basetype, Text = b.basetype });
ViewData["basetype"] = basetypes;
return View(meal);
}
}
else
{
var db = new DB();
IEnumerable<SelectListItem> basetypes = db.Basetypes.Select(
b => new SelectListItem { Value = b.basetype, Text = b.basetype });
ViewData["basetype"] = basetypes;
return View(meal);
}
}
我知道这个问题是很老了,但我今天来到这里,同样的问题,所以其他的以后可以来这里...
I know this question is very old, but I came here today with the same problem, so other could come here later...
这篇关于具有关键的“我的钥匙”的ViewData的产品类型'System.String'的,但必须是类型为“IEnumerable的&LT; SelectListItem&GT;'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!