我得到这个错误:错误:调用以下方法或属性之间的暧昧
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>
重复每行中的文本“CategoryName”是没有意义的,尤其是当您在标题中已经有该文本时。我假设你的意思是@@ Html.DisplayFor(modelItem => item.CategoryName)'(不是'DisplayNameFor()'),它将输出属性的值,而不是它的名字 –
@StephenMuecke这就是为什么他会因为他使用@ Html.DisplayNameFor()而不是@ Html.DisplayFor() – Rajput
@Rajput,是的,我知道:) –