我得到這個錯誤:錯誤:調用以下方法或屬性之間的曖昧
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,是的,我知道:) –