2011-06-23 7 views
0

私はmvcプロジェクトにいくつかのhtmlextensionsを追加しようとしました。私はそれらを使用しようとすると、彼らはすべてこのHtmlHelper htmlHelperパラメータを期待していますか?しかし、すべての例によると、これらは予想されていません。私は間違って何をしていますか?HtmlExtensions for this HtmlHelper <TModel> htmlHelper

パブリック静的文字列RadioButtonListFor(このHtmlHelperのHtmlHelperの、式>式、文字列tagBase)TModelの:クラス {リターンhtmlHelper.RadioButtonListFor(発現、tagBase、NULL); }

public static string RadioButtonListFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, RadioButtonListViewModel>> expression, String tagBase, object htmlAttributes) where TModel : class 
    { 
     return htmlHelper.RadioButtonListFor(expression, tagBase, new RouteValueDictionary(htmlAttributes)); 
    } 

    public static string RadioButtonListFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, RadioButtonListViewModel>> expression, String tagBase, IDictionary<string, object> htmlAttributes) where TModel : class 
    { 
     var inputName = tagBase; 
     RadioButtonListViewModel radioButtonList = GetValue(htmlHelper, expression); 

     if (radioButtonList == null) 
      return String.Empty; 

     if (radioButtonList.ListItems == null) 
      return String.Empty; 


     var containerTag = new TagBuilder("td"); 
     containerTag.MergeAttribute("id", inputName + "_Container"); 
     foreach (var item in radioButtonList.ListItems) 
     { 
      var radioButtonTag = RadioButton(htmlHelper, inputName, new SelectListItem { Text = item.Text, Selected = item.Selected, Value = item.Value.ToString() }, htmlAttributes); 

      containerTag.InnerHtml += radioButtonTag; 
     } 

     return containerTag.ToString(); 
    } 

答えて

0

あなたはHtmlHelperクラスの拡張メソッドを書いています。あなたが拡張メソッドを使用するこれまでとき、あなたはあなたの拡張メソッドがどの名前空間をインポートする必要があります。

セイは、たとえばRadioButtonListForのためにあなたのビューで今すぐMyNamespace

namespace MyNamespace 
{ 
    public static class HtmlExtensions 
    { 
     public static string RadioButtonListFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, RadioButtonListViewModel>> expression, String tagBase, object htmlAttributes) where TModel : class 
     { 
      return htmlHelper.RadioButtonListFor(expression, tagBase, new RouteValueDictionary(htmlAttributes)); 
     } 
    } 
} 

であるあなたがMyNamespaceをインポートする必要がこの拡張メソッドを使用する。 Razorで名前空間をインポートするには、ページの上部にこれを指定します。

@using MyNamespace 
+0

これを実行しました。私はメソッド/拡張子にアクセスできます。それは私の見解では、私がそれを使用しようとすると、それは私に最初のパラメータを要求しますが、それはしてはいけません。私は自分自身のインスタンスを渡すように頼んでいます。 –

+0

@billyあなたはビューを投稿できますか? – Eranga

+0

こんにちは、Eranga、それは今働いています。正直に言うとなぜか分からない! –

0

私は、HtmlHelper.DropDownListヘルパーの拡張メソッドを作成する記事を書いています。それを確認してください...それは助けになるかもしれません。 DropDownListDropDownListForメソッドをカバーし、Razorビューファイルとweb.configの両方で、拡張メソッドクラスの名前空間への参照を含めて説明します。

Populate html select lists from data in a view model in an ASP.NET MVC 3 application

関連する問題