In my razor view , am using @Html.ActionLink to display an hyperlink , and the text that gets displayed is hard coded( "Brand" in this case). model for the view is @model IEnumerable
Exisitng View
@Html.ActionLink("Brand", "Index", new { sortOrder = ViewBag.BrandSortParm })
Instead of hard coding the text , would like to use @Html.DisplayNameFor as first parameter in @Html.ActionLink , something like mentioned below , which is giving compile error
@Html.ActionLink(@Html.DisplayNameFor(model => model.BRAND_NAME), "Index", new { sortOrder = ViewBag.BrandSortParm })
Please let me know , how to do that.
Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.
ActionLink is rendered as an HTML Anchor Tag (HyperLink) and hence it produces a GET request to the Controller's Action method which cannot be used to send Model data (object). Hence in order to pass (send) Model data (object) from View to Controller using @Html.
DisplayNameFor :- It shows the name of the property or the name used in the display attribute of the property. So in the browser it will display 'Current User'. So for example 2 in the browser it will display 'UserName'. DisplayFor :- It is used for displaying model items or database items in the browser.
You need a string, so make it ToHtmlString()
@Html.ActionLink(Html.DisplayNameFor(model => model.BRAND_NAME).ToHtmlString(), "Index", new { sortOrder = ViewBag.BrandSortParm })
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