2010-12-02 4 views
3

는 이제 사용자 정의 HTML 도우미사용자 정의 강력한 형식의 HTML 도우미 메서드를 만드는 방법은 무엇입니까?

는 프레임 워크에 같이 강력한 형식의 도우미 메서드 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.TextBoxFor 메서드가 필요하지 않습니다. 나는 그것이 있다는 것을 알고 있습니다. 이 동작을 직접 구현하는 방법이 궁금해서 간단한 예제로 사용했습니다.

추신. 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); 
} 
를 찾을 수
관련 문제