2016-12-16 37 views
0

我得到這個錯誤:錯誤:調用以下方法或屬性之間的曖昧

The call is ambiguous between the following methods or properties:

DisplayNameFor<IEnumerable<Category>,string>(HtmlHelper<IEnumerable<Category>>, System.Linq.Expressions.Expression<System.Func<IEnumerable,string>>)

and

DisplayNameFor<Category,string>(HtmlHelper<IEnumerable>, System.Linq.Expressions.Expression<System.Func<Category,string>>)

我的模型是

public class Category 
{ 
    public int CategoryId { get; set; } 
    public string CategoryName { get; set; } 
} 

我的上下文模型

public class CategoryContext : DbContext 
{ 
    public DbSet<Category> category { get; set; } 
} 

我的控制器是:

public ActionResult GetCategory() 
{ 
    using (CategoryContext cc = new CategoryContext()) 
    { 
     var cat = cc.category.ToList(); 
     return View(); 
    } 
} 

我的看法是:將這些方法之間的曖昧上述

@model IEnumerable<CRUD_Manav_EF.Models.Category> 

<h1>Get Category</h1> 

<table> 
    <tr> 
     <th>@Html.DisplayNameFor(model => model.CategoryName)</th> 
    </tr> 
    @foreach (var item in Model) 
    { 
     <tr> 
      <td> 
       @Html.DisplayNameFor(modelItem => item.CategoryName) // I get error here 
      </td> 
      <td> 
       @Html.ActionLink("Edit", "Update", new { id = item.CategoryId }) 
       @Html.ActionLink("Details", "Details", new { id = item.CategoryId }) 
       @Html.ActionLink("Delete", "Delete", new { id = item.CategoryId }) 
      </td> 
     </tr> 
    } 
</table> 
+0

重複每行中的文本「CategoryName」是沒有意義的,尤其是當您在標題中已經有該文本時。我假設你的意思是@@ Html.DisplayFor(modelItem => item.CategoryName)'(不是'DisplayNameFor()'),它將輸出屬性的值,而不是它的名字 –

+0

@StephenMuecke這就是爲什麼他會因爲他使用@ Html.DisplayNameFor()而不是@ Html.DisplayFor() – Rajput

+0

@Rajput,是的,我知道:) –

回答

0

此錯誤顯示,因爲您在表foreach循環和呼叫使用 @Html.DisplayNameFor(model => model.CategoryName)。由於在迭代過程中一次又一次使用顯示名稱沒有好處。如果你會看到@Html.DisplayNameFor()的整個描述,你會得到第一個參數只接受模型(lambda表達式),而不接受模型的IEnumerable。這也顯示在你的編譯器錯誤中。

看到示例屏幕截圖(這是虛擬項目)

enter image description here

使用@html.DisplayFor(..)中而不是你的foreach循環。

@foreach (var item in Model) 
    { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.CategoryName) 
      </td> 
      <td> 
       @Html.ActionLink("Edit", "Update", new { id = item.CategoryId }) 
       @Html.ActionLink("Details", "Details", new { id = item.CategoryId }) 
       @Html.ActionLink("Delete", "Delete", new { id = item.CategoryId }) 
      </td> 
     </tr> 
    } 

此htmlhelper方法將採用您的模型的IEnumerable。你的問題將得到解決(你可以自己檢查)。

+0

感謝buddy它解決了,我需要一些幫助認證和授權與mvc(自定義) –

+0

請將此答案標記爲已接受的答案並向上投票,以便它對其他用戶有用謝謝@ManavPandya – Rajput

相關問題