无法将System.Data.Entity.Infrastructure.DbQuery <selectlistitem>类型转换为System.Collections.Generic.List <selectlistitem>

时间:2017-02-16 21:11:34

标签: asp.net-mvc asp.net-mvc-4

我有以下观点,我正在尝试从ViewBag.Catagories构建下拉列表:

 @Html.DropDownListFor(x => x.CategoryId, List<SelectListItem>)ViewBag.Catagories,
     new { @class = "form-control" })

这里是我有一个带有列表的ViewBag的Action:

public ActionResult CreateEdit(int id = 0)
{
    var context = new FeedbackContext();
    ViewBag.Catagories = context.Categories.Select(
        x => new SelectListItem() { Text = x.Name, Value = x.Id.ToString() }).ToList();
    if (id != 0)
    {
        return View(context.Tasks.FirstOrDefault(x => x.Id == id));
    }
 }

在这里,它可以首次填充下拉列表中的类别,但是当我验证/重新提交它时,它将引发错误。

  

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:无法转换类型&#39; System.Data.Entity.Infrastructure.DbQuery&lt; System.Web.Mvc.SelectListItem&gt;&#39;到&#39; System.Collections.Generic.List&lt; System.Web.Mvc.SelectListItem&gt;&#39;

请说明我在这里收到错误的原因。

0 个答案:

没有答案
相关问题