简体   繁体   中英

query in Asp.net mvc5

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 ?

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM