2016-12-16 5 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.DisplayFor()の代わりに@ Html.DisplayName()を使用しています。 – Rajput

+0

@Rajput、はい私は知っています:) –

答えて

0

あなたは、上記のものの方法の間の曖昧な希望テーブルforeachループと呼び出しで @Html.DisplayNameFor(model => model.CategoryName)を使用していたため、このエラーが表示されます。反復処理中は、表示名を何度も何度も使用するメリットはないためです。 @Html.DisplayNameFor()の全体的な説明が表示される場合は、最初のパラメータはモデル(ラムダ式)のみを受け取り、モデルのIEnumerableは受け入れないことになります。これはコンパイラのエラーでも表示されます。

ではなく、あなたのforeachループで(これはダミーのプロジェクトです)

enter image description here

使用@html.DisplayFor(..)例のスクリーンショットでご覧ください。

@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

おかげで解決された、私は認証& mvc(カスタム) –

+0

この回答に「受け入れられた回答」とマークして、他のユーザーにとって便利なようにupvoteしてください。Thanks @ManavPandya – Rajput

関連する問題