2016-12-16 65 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