- Yes - How can I override the @Html.LabelFor template?
- 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.
MVC Html Helpers - How much control over inbuilt ones?
-
31-05-2023 - |
Domanda
I have some questions about html helpers in ASP.NET MVC Framework. So...
- Can I override a inbuilt helper like
@Html.HiddenFor
? - Should I
override
, or create aCustomHiddenFor
one? While creating a
Custom
one, how can I change the value fromModelMetaData
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); }
Soluzione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow