2010-12-02 6 views
3

は今、私は今ではフレームワークであると同じように強く型付けされたヘルパーメソッドInputForにそれを有効にする方法強く型付けされたカスタムヘルパーメソッドを作る方法は?

using System; 
namespace MvcApplication.Helpers { 
    public class InputlHelper { 
    public static string Input(this HtmlHelper helper, string name, string text) { 
     return String.Format("<input name='{0}'>{1}</input>", name, text); 
    } 
    } 
} 

カスタムHTMLヘルパーを作成する方法を見つけましたか?

私はHtml.TextBoxForメソッドを必要としません、私はそれが存在することを知っています。私はこの動作を自分自身で実装する方法が不思議で、これを簡単な例として使用しました。

PS。私はmvcのソースコードを見ていましたが、この謎のトレースを見つけることができませんでしたTextBoxFor。私はTextBoxしか見つけられませんでした。 codeが間違っていますか?

答えて

4

ここにASP.NET MVC 2 RTM Source codeがあります。

あなたはSystem.Web.Mvc.Html名前空間内InputExtensionsクラスを見てみると次のコード

[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")] 
public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) { 
    return htmlHelper.TextBoxFor(expression, (IDictionary<string, object>)null); 
} 

[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")] 
public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes) { 
    return htmlHelper.TextBoxFor(expression, new RouteValueDictionary(htmlAttributes)); 
} 

[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")] 
public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes) { 
    return TextBoxHelper(htmlHelper, 
          ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model, 
          ExpressionHelper.GetExpressionText(expression), 
          htmlAttributes); 
} 
を見つけるだろう
関連する問題