2014-10-19 26 views
0

所以是的,我已經調查和閱讀了很多答案,但他們都與數據類型不匹配..我無法弄清楚爲什麼會發生這種情況。流行的模型項目傳入類型System.Collections.Generic.List`1 [X]的指令,但這個字典需要一個X型的模型項目

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{} 

// CODE

所以..年底我有一個模型。我在視圖中傳遞了一組項目,視圖在mvc的LIST模板下,並且它表示我傳遞了一個通用列表,但它需要一個對象?是什麼賦予了?? ..

這是錯誤

傳遞到字典的模型項的類型爲「System.Collections.Generic.List`1 [OrdenarBackEnd.Models.RestaurantBranchModel]」,但這個字典需要一個'OrdenarBackEnd.Models.UserModel'類型的模型項目。

+0

什麼是堆棧跟蹤? – SLaks 2014-10-19 18:14:47

+0

'_XenonLayoutPage.cshtml'採用哪種型號? – SLaks 2014-10-19 18:15:09

回答

0

變化

@Html.DisplayNameFor(model => model.name) 

@Html.DisplayNameFor(model => model.First().name) 

基本上它需要一個對象,你傳遞一個列表。

+0

如果這是問題,他會得到一個編譯器錯誤。 – SLaks 2014-10-19 18:20:57

相關問題