For the following ActionLink call:
@Html.ActionLink("Customer Number", "Search", new { Search = ViewBag.Search, q = ViewBag.q, sortOrder = ViewBag.CustomerNoSortParm, })
I'm trying to pass in the label for @model.CustomerNumber to generate the "Customer Number" text instead of having to pass it in explicitly. Is there an equivilant of @Html.LabelFor(model => model.CustomerNumber ) for parameters?
There is no such helper out of the box.
But it's trivially easy to write a custom one:
public static class HtmlExtensions
{
public static string DisplayNameFor<TModel, TProperty>(
this HtmlHelper<TModel> html,
Expression<Func<TModel, TProperty>> expression
)
{
var htmlFieldName = ExpressionHelper.GetExpressionText(expression);
var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
return (metadata.DisplayName ?? (metadata.PropertyName ?? htmlFieldName.Split(new[] { '.' }).Last()));
}
}
and then use it (after bringing the namespace in which you defined it into scope):
@Html.ActionLink(
"Customer Number",
"Search",
new {
Search = ViewBag.Search,
q = ViewBag.q,
sortOrder = ViewBag.CustomerNoSortParm,
customerNumberDescription = Html.DisplayNameFor(model => model.CustomerNumber)
}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With