user2915962
user2915962

Reputation: 2711

Making link of html.helper

Im creating a simple CMS for webpages...When i have created a new page i dsiplay the page in a view like this:

@Html.DisplayNameFor(model => model.Url)

Can i embedd this pice of code in an anchor tag and point it to the proper adress.

Lets say i have created a page called games that have the model.url = "/games" The way it is now, i have to write:

http://localhost:xxx7/games

To get to it...I would really like to just click on it.

EDIT: Thank you for answering! Im sure the way you are describing is in the right direction. The thing is this, the page where i display my:

@Html.DisplayNameFor(model => model.Url)

Contains a table with maybe 10 different:

@Html.DisplayNameFor(model => model.Url)

If i do:

@Html.ActionLink( @Html.DisplayNameFor(model => model.Url).ToHtmlString(), "Index", "Games")

They all will poit to the same place, i need it to be dynamic so that each:

@Html.DisplayNameFor(model => model.Url)

Points to the url it represents.

Upvotes: 0

Views: 182

Answers (2)

jb.
jb.

Reputation: 1918

The usual way would be to use the Html.ActionLink to specify the controller and view.

@Html.ActionLink( "Display Name", "ActionName", "ControllerName")

So you could do something like:

@Html.ActionLink( @Html.DisplayNameFor(model => model.Url).ToHtmlString(), "Index", "Games")

But I see no reason you couldn't just drop it inside an anchor if you are generating the url's some other way - or if it is external to your own page:

<a href="@Html.DisplayNameFor(model => model.Url)">DisplayNameFor(model => model.Url)</a>

Upvotes: 1

rosko
rosko

Reputation: 474

Read about: http://www.w3schools.com/aspnet/mvc_htmlhelpers.asp @Html.ActionLink()

@Html.ActionLink("Link name or whatever", "your_action", "your_controller")

More information on: http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink(v=vs.118).aspx

Upvotes: 3

Related Questions