So yeah I have investigated and read a lot of responses here but they all have mismatch with data types.. I cannot figure out why this is happening.
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; }
}
Controller
RestaurantBranchRepository RestaurantBranchRepository = new RestaurantBranchRepository();
IEnumerable<RestaurantBranchModel> Branches;
// GET: RestaurantBranch
public ActionResult Index()
{
Branches = RestaurantBranchRepository.GetBranches();
return View(Branches); //I've also tried adding .ToList()
}
View
@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{}
//END OF CODE
So.. I have a the model. I pass a collection of items in the View, the View is under the LIST template for mvc, and it says i am passing a generic list but it needs a single object ? what gives??..
This is the error
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[OrdenarBackEnd.Models.RestaurantBranchModel]', but this dictionary requires a model item of type 'OrdenarBackEnd.Models.UserModel'.
Change
@Html.DisplayNameFor(model => model.name)
To
@Html.DisplayNameFor(model => model.First().name)
Basically it needs a single object and you are passing in a list.
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.