質問

I have some questions about html helpers in ASP.NET MVC Framework. So...

  1. Can I override a inbuilt helper like @Html.HiddenFor?
  2. Should I override, or create a CustomHiddenFor one?
  3. While creating a Custom one, how can I change the value from ModelMetaData and then call the inbuilt helper? Eg:

    public static MvcHtmlString HiddenSecuredFor<TModel, TProperty>
        (this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes,bool secured)
    {
        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        var name = ExpressionHelper.GetExpressionText(expression);
        if (!secured)
        {
            return htmlHelper.HiddenFor(expression,htmlAttributes);
        }
    
            // Here I want to change the value 
            // from the TModel lets say the property is x => x.Name
            // And I want to make the Name = "Mr. " + Name
    
        // Call the inbuild helper with the expression value changes.
        return htmlHelper.HiddenFor(expression, htmlAttributes);
    }
    
役に立ちましたか?

解決

  1. Yes - How can I override the @Html.LabelFor template?
  2. It depends - If you are adding functionality you always need and you want other developers to use without having to know about it, overriding is a good option. Otherwise, creating a custom one that calls the original is a good approach.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top