我试图将selectList插入到视图(表单)中。我想我会通过在控制器中填充列表并将其作为视图包发送到视图来完成此操作。这里就是我有这么远:SelectListItem在加载时崩溃
var query = from p in db.ProductCategories
join pt in db.ProductCategoriesTranslations on p.ProductCategoriesId equals pt.ProductCategoriesId
where pt.ProductLanguage.Equals("se")
orderby pt.ProductCategoriesName
select new SelectListItem
{
Value = p.ProductCategoriesId.ToString(),
Text = pt.ProductCategoriesName
};
ViewBag.ProductCategoriesId = query;
return View();
然后在视图中我有:
@Html.DropDownList("ProductCategoriesId", String.Empty)
我认为这是简单而直接的,但是当我加载它与下面的错误崩溃的事情:
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
有什么建议吗?
谢谢,这个工作,但它的工作没有在视图中的更正,所以这段代码工作,即使它不应该? '@ Html.DropDownList(“ProductCategoriesId”,string.Empty)' – Dennis 2013-02-08 13:24:35
它可能在默认情况下使用ViewBag。我不知道这是因为我总是使用ViewModels来处理我的视图。 – 2013-02-08 13:30:29
我早先尝试了SqlFunctions.StringConvert,但又得到了另一个错误信息。我想这主要是我不知道如何正确实施它。通过您提供的解决方案使用它还有一些优势吗? – Dennis 2013-02-09 08:31:35