【发布时间】: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.List
1[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