Select specific columns using EF in MVC 5

Jaber Kibria

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.List1[<>f__AnonymousType16[System.String,System.String,System.String,System.Nullable1[System.DateTime],System.Nullable1[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);
}

Here is the result in console

Nkosi

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.

edited at
0

Comments

0 comments
Login to comment

Related

Select specific columns in pandas DataFrame

Laravel 5 Eloquent scope join and select specific columns

select specific columns when using include statement with entity framework

Intersect Select Statements on Specific Columns

sqlalchemy: select specific columns from multiple join using aliases

Select only specific columns of CSV

Using repository patern when using async / await methods Asp.NET MVC5 EF

Select specific columns in NumPy array using colon notation

Perl: Using an array list in order to select specific columns

Ruby: Using an array list in order to select specific columns

EF core Database specific columns to nested object

asp.net mvc - LINQ Query to select specific columns

Update specific fields using EF in asp.net MVC

How to select values in a multiple DropDownList using DropdownListFor in MVC 5

How to Select Specific Columns in Pandas DataFrame Using Multiple Boolean Conditions

MVC 5 - How to update relational table using EF code first

Sum Specific Columns for Select Statement

MVC 4 specific columns for roles

Using DISTINCT for specific columns

Insert decimal value into SQL Server using EF 5 and MVC 4

MVC5 EF CodeFirst Select based on statement

How to return to a specific tab in a view using MVC 5 Bootstrap tabs

Select specific columns determined by the headers

How to select specific country from dropdown list in EF when page loads in asp.net mvc

Select columns that have a specific type

Using Objection JS How can I select specific columns with withGraphFetched

Select specific columns with a condition in Laravel

How to select specific columns in r using regex

Select specific/all columns in rowwise