我想在MVC 5中使用Entity Framework
來獲得一些特定的列值。但是它向我顯示了錯誤。這是我的控制器的方法的代碼:在MVC 5中使用EF選擇特定的列5
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__AnonymousType1
6 [系統。 String,System.String,System.String,System.Nullable1[System.DateTime],System.Nullable
1 [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
@Nkosi,在哪裏編輯我的代碼? –