【问题标题】:query in Asp.net mvc5在 Asp.net mvc5 中查询
【发布时间】:2017-10-25 07:45:53
【问题描述】:
public ActionResult Index()
{
    return View(db.Employees.Select(x => new { x.First_Name, x.Last_Name, 
         x.Email }).ToList());
}

我的表 Employee 中有 5 列,它们是 First_NameLast_NameAddressSalaryEmail。在这 5 列中,我只想在浏览器中显示 First_NameLast_NameEmail。 这是我正在使用的视图

@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>

我收到的错误是:期间生成了未处理的异常 当前网络请求的执行。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。

我是 asp.net Mvc 的初学者。我应该怎么做?

【问题讨论】:

  • 您能否提供异常详细信息和错误堆栈跟踪?我认为db.Employees.ToList() 就足够了,您返回的是匿名类型而不是Employee 表属性成员。
  • 您的查询返回匿名对象的集合,而不是Employee 的集合只需删除您的.Select(...) 并仅在视图中显示您想要的Employee 的那些属性(但是为什么您有DisplayFor() 用于属性 AddressSalary 如果您不想要它们?)
  • 您正在向视图发送匿名类型而不是 Employee。您需要从 Employee 创建一个具有所需属性的 DTO 类,然后将视图更改为 @model IEnumerable&lt;YourEmployeeDTO&gt; 像这样 stackoverflow.com/a/46905481/2946329
  • 异常详细信息:System.InvalidOperationException:传入字典的模型项的类型为“System.Collections.Generic.List1[&lt;&gt;f__AnonymousType43[System.String,System.String,System.String] ]',但此字典需要类型为“System.Collections.Generic.IEnumerable`1[crudOperation.Employee]”的模型项。
  • 堆栈跟踪:[InvalidOperationException:传入字典的模型项的类型为“System.Collections.Generic.List1[&lt;&gt;f__AnonymousType43[System.String,System.String,System.String]] ',但此字典需要类型为 'System.Collections.Generic.IEnumerable`1[crudOperation.Employee]' 的模型项。]

标签: c# asp.net-mvc entity-framework linq


【解决方案1】:

您正在向视图发送匿名对象而不是 Employee 列表。

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

由于不鼓励将域模型 Employee 直接发送到视图,因为您可能会公开更多不想显示的属性,因此我将创建 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; }
}

控制器

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);
}

查看

@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>

【讨论】:

    【解决方案2】:

    您正在返回一个非 Employee 对象的集合。如果你想要返回 List,你可以使用 _db.Employees.ToList(),如果不是,你必须创建一个 DTO 类,其中包含你想要返回的属性。

    【讨论】:

      猜你喜欢
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      • 2014-03-13
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多