所以是的,我已经调查并阅读了很多回复,但它们都与数据类型不匹配。我无法弄清楚为什么会发生这种情况。
public class RestaurantBranchModel
{
public int id { get; set; }
public string name { get; set; }
public string telephone { get; set; }
public int master_restaurant_id { get; set; }
//public RestaurantModel master_restaurant { get; set; }
public int address_id { get; set; }
//public AddressModel address { get; set; }
}
控制器
RestaurantBranchRepository RestaurantBranchRepository = new RestaurantBranchRepository();
IEnumerable<RestaurantBranchModel> Branches;
// GET: RestaurantBranch
public ActionResult Index()
{
Branches = RestaurantBranchRepository.GetBranches();
return View(Branches); //I've also tried adding .ToList()
}
查看
@model IEnumerable<OrdenarBackEnd.Models.RestaurantBranchModel>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_XenonLayoutPage.cshtml";}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.name)
</th>
<th>
@Html.DisplayNameFor(model => model.telephone)
</th>
<th>
@Html.DisplayNameFor(model => model.master_restaurant_id)
</th>
<th>
@Html.DisplayNameFor(model => model.address_id)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.telephone)
</td>
<td>
@Html.DisplayFor(modelItem => item.master_restaurant_id)
</td>
<td>
@Html.DisplayFor(modelItem => item.address_id)
</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>
@section BottomScripts{}
//代码结束
所以......我有一个模特。我在视图中传递了一组项目,视图在mvc的LIST模板下,它说我传递的是通用列表,但它需要一个对象?什么给了?? ..
这是错误
传递到字典中的模型项的类型为&#39; System.Collections.Generic.List`1 [OrdenarBackEnd.Models.RestaurantBranchModel]&#39;,但此字典需要类型为&#39的模型项; OrdenarBackEnd.Models.UserModel&#39;
答案 0 :(得分:0)
更改
@Html.DisplayNameFor(model => model.name)
要
@Html.DisplayNameFor(model => model.First().name)
基本上它需要一个对象而你正在传递一个列表。