【发布时间】:2017-05-10 10:45:05
【问题描述】:
我想在 MVC 5 中使用 Entity Framework 获取一些特定的列值。但它显示错误。这是我的控制器方法代码:
public ActionResult Index()
{
var systemUsers = db.SystemUsers
.Include(s => s.SystemUser1)
.Select(s => new {
s.FullName,
s.Email,
s.Image,
s.UpdateDate,
s.UpdatedBy,
s.Id
});
return View(systemUsers.ToList());
}
这是错误信息:
传入字典的模型项的类型为'System.Collections.Generic.List
1[<>f__AnonymousType16[System.String,System.String,System.String,System.Nullable1[System.DateTime],System.Nullable1[System.Int32] ,System.Int32]]',但此字典需要类型为“System.Collections.Generic.IEnumerable`1[MVC.Models.SystemUser]”的模型项。
再次当我无法获得特定列的单个结果时。默认情况下,控制器方法在尝试使用ajax 时也会返回意外的外键数据。
这是单个结果的代码。
[HttpPost]
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
SystemUser systemUser = db.SystemUsers.Find(id);
return Json(systemUser);
}
【问题讨论】:
-
您听说过视图模型吗?谷歌它
-
视图需要强类型视图模型,但您传递的是匿名类型。
-
@Nkosi,在哪里编辑我的代码?
标签: c# asp.net-mvc entity-framework asp.net-mvc-5