Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Html.DisplayNameFor within Html.ActionLink (MVC 4)

Tags:

asp.net-mvc

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.

like image 320
refactor Avatar asked Nov 06 '13 11:11

refactor


People also ask

What is HTML ActionLink in MVC?

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.

How do you pass data from view to controller using ActionLink?

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.

What is the difference between DisplayNameFor and DisplayFor in MVC?

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.


1 Answers

You need a string, so make it ToHtmlString()

@Html.ActionLink(Html.DisplayNameFor(model => model.BRAND_NAME).ToHtmlString(), "Index", new { sortOrder = ViewBag.BrandSortParm })
like image 139
Zabavsky Avatar answered Oct 10 '22 12:10

Zabavsky