【问题标题】:MVC return list of items to the viewMVC 将项目列表返回到视图
【发布时间】:2014-04-30 00:04:35
【问题描述】:

谁能帮帮我。我正在尝试从数据库中检索存储详细信息列表并简单地在视图中显示该列表。

存储模型:

 public class StorageModel
{
    [Required]
    [Display(Name = "Storage Name")]
    public string Name { get; set; }

    [Required]
    [Display(Name = "Date From")]
    public string DateFrom { get; set; }

    [Required]
    [Display(Name = "Date To")]
    public string DateTo { get; set; }

    [Required]
    [Display(Name = "Size")]
    public string Size { get; set; }
}

控制器方法:

 public ActionResult ViewStorage()
    {
        List<CommonLayer.TblNewsStorage> storageList = new BusinessLayer.Storage().getAllStorage().ToList();
        return View(storageList);
    }

正在从上面的 BusinessLayer 中检索数据:

public IQueryable<CommonLayer.TblNewsStorage> getAllStorage()
    {
        return this.Entities.TblNewsStorage;
    }

现在我使用视图脚手架模板使用 StorageModel 创建了一个强类型视图,但是它不起作用。我到底做错了什么?我尝试通过 var 而不是 List 但它仍然无法正常工作。我是 MVC 的新手,所以我一定做错了什么。向视图传递和显示数据列表的正确方法是什么?

查看MVC生成的代码:

    @model IEnumerable<NewsLibrary.Models.StorageModel>

@{
    ViewBag.Title = "ViewStorage";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>ViewStorage</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DateFrom)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DateTo)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Size)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DateFrom)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DateTo)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Size)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>

我收到以下错误:

传入字典的模型项的类型为“System.Collections.Generic.List1[CommonLayer.TblNewsStorage]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[NewsLibrary.Models.StorageModel]”。

【问题讨论】:

  • 什么不起作用?运行时会发生什么?
  • 请粘贴您的错误转储
  • @Shoe 抱歉,我忘了粘贴错误。更新错误
  • 错误是不言自明的。您正在传递一个 CommonLayer.TblNewsStorage 列表,但您的视图采用 NewsLibrary.Models.StorageModel 的模型。确保两者使用相同的模型。

标签: c# asp.net-mvc


【解决方案1】:

@model IEnumerable<NewsLibrary.Models.StorageModel>

必须传入相同的数据类型。

你来了

List<CommonLayer.TblNewsStorage>

【讨论】:

    【解决方案2】:

    如果您仔细查看 View 期望的 Model,您会看到它是 NewsLibrary.Models.StorageModel 的 IEnumerable。您正在传递 CommonLayer.TblNewsStorage 类型的列表/IEnumerable。确保这两个是相同的数据类型。

    【讨论】:

    • 模型基于类型 CommonLayer.TblNewsStorage(直接从数据库中检索),我可以进行某种转换吗?
    • 使用类似 automapper 的东西来进行自动映射。
    • 我应该在将它们传递给视图之前在控制器中转换它们,或者为视图制作一个特殊的 ViewModel :)。 stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc
    【解决方案3】:

    @foreach(模型中的变量项){ 项目名 }

    试试这样。您需要将 @html.displayfor(model => item.name) 删除为标签内的只有 item.name

    【讨论】:

    • 不,DisplayName 实际上是在外面。该函数返回显示名称(也称为变量名称,如果它没有 DisplayName 属性)
    猜你喜欢
    • 2013-11-19
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多