I want to get some specific column values using Entity Framework
in MVC 5. But it's showing me error. Here is my code for controller method:
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());
}
This is the error message:
The model item passed into the dictionary is of type 'System.Collections.Generic.List
1[<>f__AnonymousType1
6[System.String,System.String,System.String,System.Nullable1[System.DateTime],System.Nullable
1[System.Int32],System.Int32]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MVC.Models.SystemUser]'.
Again when I'm unable to get single result with specific columns. By default controller method also returning unexpected foreign key data while trying to get using ajax
. Here is the code for single result.
[HttpPost]
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
SystemUser systemUser = db.SystemUsers.Find(id);
return Json(systemUser);
}
The view requires a strongly typed view model but you are passing an anonymous type
Update the select to return a strongly typed object collection.
public ActionResult Index()
{
var systemUsers = db.SystemUsers
.Include(s => s.SystemUser1)
.Select(s => new SystemUser { //<--HERE
FullName = s.FullName,
Email = s.Email,
Image = s.Image,
UpdateDate = s.UpdateDate,
UpdatedBy = s.UpdatedBy,
Id = s.Id
});
return View(systemUsers.ToList());
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments