2

誰かがHtml.TextBoxForヘルパーを拡張する簡単でわかりやすい例を提供してくれることを願っています。trueの場合にコントロールを読み取り専用にする(サプライズ、サプライズ、サプライズ)ブール値のReadOnlyパラメーターを含めたいと思います。うまくいかなかった例をいくつか見て、次のことを試しましたが、HtmlHelperパラメーターが認識するTextBoxForの唯一の署名は、作成しているものです(usingステートメントがありませんか?) :

public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes, bool disabled)
    {
        var values = new RouteValueDictionary(htmlAttributes);

        if (disabled)
            values.Add("disabled", "true");

        return htmlHelper.TextBoxFor(expression, values)); //<-- error here
    }

簡単な例が私を正しい軌道に乗せるのに役立つことを願っています。

ありがとう。

4

2 に答える 2

2

using System.Web.Mvc.Html;を呼び出すために拡張クラスにいることを確認してくださいHtmlHelper.TextBoxFor<>

public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, 
    Expression<Func<TModel, TProperty>> expression, 
    object htmlAttributes, 
    bool disabled)
    {
        var values = new RouteValueDictionary(htmlAttributes);
        // might want to just set this rather than Add() since "disabled" might be there already
        if (disabled) values["disabled"] = "true";
        return htmlHelper.TextBoxFor<TModel, TProperty>(expression, htmlAttributes);
    }
于 2010-09-01T13:08:18.317 に答える
1

You have one opening and two closing parenthesis on this line. Should be:

return htmlHelper.TextBoxFor(expression, values);

Also to make your HTML a little more standards friendly:

values["disabled"] = "disabled";
于 2010-09-01T14:27:38.173 に答える