developer tip

Razor로 일반 @helper 메서드를 만들 수 있습니까?

optionbox 2020. 9. 16. 07:36
반응형

Razor로 일반 @helper 메서드를 만들 수 있습니까?


Razor에서 다음과 같은 도우미를 작성하려고합니다.

@helper DoSomething<T, U>(Expression<Func<T, U>> expr) where T : class

불행히도 파서 <T그것이 HTML 요소의 시작 이라고 생각 하고 구문 오류로 끝납니다. 일반 메서드 인 Razor로 도우미를 만들 수 있습니까? 그렇다면 구문은 무엇입니까?


아니요, 불가능합니다. 대신 일반 HTML 도우미를 작성할 수 있습니다.

public static MvcHtmlString DoSomething<T, U>(
    this HtmlHelper htmlHelper, 
    Expression<Func<T, U>> expr
) where T : class
{
    ...
}

그리고:

@(Html.DoSomething<SomeModel, string>(x => x.SomeProperty))

또는 모델을 첫 번째 일반 인수로 타겟팅하는 경우 :

public static MvcHtmlString DoSomething<TModel, TProperty>(
    this HtmlHelper<TModel> htmlHelper, 
    Expression<Func<TModel, TProperty>> expr
) where T : class
{
    ...
}

이렇게하면 다음과 같이 호출 할 수 있습니다 (물론 뷰가 강력하게 입력되었다고 가정하지만 모든 뷰가 어쨌든 강력하게 입력되어야하므로 안전한 가정입니다 :-)) :

@Html.DoSomething(x => x.SomeProperty)

이것은 이다 와 헬퍼 파일 안에 달성 할 수 @functions구문하지만 당신이 원하는 경우에 당신이 언급하는 면도칼 스타일의 가독성 또한 HTML의 적합과 끝을 할 수있는 일반 도우미를 호출해야합니다.

도우미 파일의 함수는 정적이므로 해당 메서드를 사용하려는 경우 페이지에서 HtmlHelper 인스턴스를 계속 전달해야합니다.

예 : Views \ MyView.cshtml :

@MyHelper.DoSomething(Html, m=>m.Property1)
@MyHelper.DoSomething(Html, m=>m.Property2)
@MyHelper.DoSomething(Html, m=>m.Property3)

App_Code \ MyHelper.cshtml :

@using System.Web.Mvc;
@using System.Web.Mvc.Html;
@using System.Linq.Expressions;
@functions
{
    public static HelperResult DoSomething<TModel, TItem>(HtmlHelper<TModel> html, Expression<Func<TModel, TItem>> expr)
    {
        return TheThingToDo(html.LabelFor(expr), html.EditorFor(expr), html.ValidationMessageFor(expr));
    }
}
@helper TheThingToDo(MvcHtmlString label, MvcHtmlString textbox, MvcHtmlString validationMessage)
{
    <p>
        @label
        <br />
        @textbox
        @validationMessage
    </p>
}
...

모든 경우에 TModel동일 할 것이고 (뷰에 대해 선언 된 모델), 제 경우 TValue에는 동일 할 것이므로 Expression 인수 유형을 선언 할 수있었습니다.

@helper FormRow(Expression<Func<MyViewModel, MyClass>> expression) {
  <div class="form-group">
    @(Html.LabelFor(expression, new { @class = "control-label col-sm-6 text-right" }))
    <div class="col-sm-6">
      @Html.EnumDropDownListFor(expression, new { @class = "form-control" })
    </div>
    @Html.ValidationMessageFor(expression)
  </div>
}

모델 필드는 모든 경우 string에, 당신은 대체 할 수 있습니다 MyClassstring.

It might not be bad to define two or three helpers with the TValue defined, but if you have any more that would generate some ugly code, I didn't really find a good solution. I tried wrapping the @helper from a function I put inside the @functions {} block, but I never got it to work down that path.


if your main problem is to get name attribute value for binding using lambda expression seems like the @Html.TextBoxFor(x => x.MyPoperty), and if your component having very complex html tags and should be implemented on razor helper, then why don't just create an extension method of HtmlHelper<TModel> to resolve the binding name:

namespace System.Web.Mvc
{
    public static class MyHelpers
    {
        public static string GetNameForBinding<TModel, TProperty>
           (this HtmlHelper<TModel> model, 
            Expression<Func<TModel, TProperty>> property)
        {
            return ExpressionHelper.GetExpressionText(property);
        }
    }
}

your razor helper should be like usual:

@helper MyComponent(string name)
{
    <input name="@name" type="text"/>
}

then here you can use it

@TheHelper.MyComponent(Html.GetNameForBinding(x => x.MyProperty))

참고URL : https://stackoverflow.com/questions/4760783/is-it-possible-to-create-a-generic-helper-method-with-razor

반응형