0
public ActionResult Index()
{
    return View(db.Employees.Select(x => new { x.First_Name, x.Last_Name, 
         x.Email }).ToList());
}

There are 5 columns in my table Employee which are First_Name, Last_Name, Address, Salary,Email. Out of these 5 column I want to display only First_Name, Last_Name and Email in Browser. This is View I am using

@model IEnumerable<crudOperation.Employee>
@{
ViewBag.Title = "Index";
}

<h2>Index</h2>


        @Html.DisplayNameFor(model => model.First_Name)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Last_Name)
    </th>

    <th>
        @Html.DisplayNameFor(model => model.Email)
    </th>
    <th></th>
</tr>

    @foreach (var item in Model) {
    <tr>
    <td>
        @Html.DisplayFor(modelItem => item.First_Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Last_Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Email)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
        @Html.ActionLink("Details", "Details", new { id=item.ID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.ID })
    </td>
    </tr>
    }
    </table>

I am getting the error as : An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

I am beginner to asp.net Mvc. How should I proceed ?

9
  • Can you provide exception details and stack trace to the error? I think db.Employees.ToList() is enough, you're returning anonymous type instead of Employee table property members. Commented Oct 25, 2017 at 7:46
  • Your query returns a collection of anonymous objects, not a collection of Employee Just remove your .Select(...) and display only those properties of Employee you want in the view (but why do you have DisplayFor() for properties Address and Salary if you do not want them?) Commented Oct 25, 2017 at 7:49
  • You are sending an anonymous type instead of Employee to the view. you need to create a DTO class with needed properties from the Employee and then change the view to @model IEnumerable<YourEmployeeDTO> like this stackoverflow.com/a/46905481/2946329 Commented Oct 25, 2017 at 7:49
  • 1
    Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List1[<>f__AnonymousType43[System.String,System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[crudOperation.Employee]'. Commented Oct 25, 2017 at 7:56
  • Stack Trace: [InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List1[<>f__AnonymousType43[System.String,System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[crudOperation.Employee]'.] Commented Oct 25, 2017 at 7:57

2 Answers 2

2

You're sending anonymous object instead of list of Employee to the view.

db.Employees.Select(x => new { x.First_Name, x.Last_Name, 
         x.Email })

Since sending the domain model Employee directly to the view is discouraged as you might expose more properties you don't want to display, I would create view model for only information you need instead.

View Model

public class EmployeeViewModel
{
    public int EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
}

Controller

public ActionResult Index()
{
    var vm = new List<EmployeeViewModel>();

    foreach (var employee in db.Employees)
    {
        vm.Add(new EmployeeViewModel
        {
            EmployeeId = employee.ID,
            FirstName = employee.First_Name,
            LastName = employee.Last_Name,
            Email = employee.Email
        });
    }

    return View(vm);
}

View

@model IList<crudOperation.EmployeeViewModel>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<table>
    <thead>
        <tr>
            <th>First name</th>
            <th>Last name</th>
            <th>Email</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var employee in Model)
        {
            <tr>
               <td>@employee.FirstName</td>
               <td>@employee.LastName</td>
               <td>@employee.Email</td>
               <td>
                   @Html.ActionLink("Edit", "Edit", new { id= employee.EmployeeId }) |
                   @Html.ActionLink("Details", "Details", new { id= employee.EmployeeId }) |
                   @Html.ActionLink("Delete", "Delete", new { id= employee.EmployeeId })
               </td>
            </tr>
        }
    </tbody>
</table>
Sign up to request clarification or add additional context in comments.

Comments

0

You are returning a collection of non Employee object. If you want return List you can use _db.Employees.ToList(), if not you must create a DTO class with properties you want to return.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.