0

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

public static string RadioButtonListFor(this HtmlHelper htmlHelper, Expression> expression, String tagBase) where TModel : class { return htmlHelper.RadioButtonListFor(expression, 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();
    }
4

2 に答える 2

0

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

ASP.NETMVC3アプリケーションのビューモデルのデータからhtml選択リストにデータを入力します

于 2011-06-23T23:33:10.080 に答える
0

HtmlHelperクラスの拡張メソッドを作成しています。拡張メソッドを使用するときはいつでも、拡張メソッドが含まれている名前空間をインポートする必要があります。

たとえばRadioButtonListForMyNamespace

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
于 2011-06-23T23:20:56.380 に答える