I want to call the @Html.ActionLink method inside a c# function to return a string with a link on it.
Something like this:
string a = "Email is locked, click " + @Html.ActionLink("here to unlock.", "unlock") ;
I want to call the @Html.ActionLink method inside a c# function to return a string with a link on it.
Something like this:
string a = "Email is locked, click " + @Html.ActionLink("here to unlock.", "unlock") ;
Maybe try this:
You could create an HtmlHelper extension method:
or if you mean to generate this link outside of the scope of an aspx page you'll need to create a reference to an HtmlHelper and then generate. I do this in a
UrlUtility
static class (I know, people hate static classes and the word Utility, but try to focus). Overload as necessary:Then you can write the following from wherever your heart desires:
There's a couple things bad about these other answers...
Shark's answer requires you to bring the LinkExtensions namespace into C#, which is not wrong, but undesirable to me.
Hunter's idea of making a helper is a better one, but still writing a helper function for a single URL is cumbersome. You could write a helper to help you build strings that accepted parameters, or you could simply do it the old fashion way:
@counsellorben,
i see no reason for the complexity; the user wants only to render an Action's routing into a hard string containing an anchor tag. Moreover, ActionLink() in a hard-written concatenated string buys one nothing, and forces the developer to use LinkExtensions whidh are intended for Views.
If the user is diehard about using ActionLink() or does not need (for some reason) to calculate this string in the constructor, doing so in a view is much better.
I still stand by and recommend the answers tvanfosson and I provided.
Assuming that you want to accomplish this in your controller, there are several hoops to jump through. You must instantiate a
ViewDataDictionary
and aTempDataDictionary
. Then you need to take theControllerContext
and create anIView
. Finally, you are ready to create yourHtmlHelper
using all of these elements (plus yourRouteCollection
).Once you have done all of this, you can use
LinkExtensions.ActionLink
to create your custom link. In your view, you will need to use@Html.Raw()
to display your links, to prevent them from being HTML encoded. Here is the necessary code:Having shown all of this, I will caution you that it is a much better idea to do this in your view. Add the error and other information to your ViewModel, then code your view to create the link. If this is needed across multiple views, create an HtmlHelper to do the link creation.
UPDATE
To address one.beat.consumer, my initial answer was an example of what is possible. If the developer needs to reuse this technique, the complexity can be hidden in a static helper, like so:
Then, in a controller, it is used like so:
or like so:
While it is easier to use
Url.Action
, this now extends into a powerful tool to generate any mark-up within a controller using all of the HtmlHelpers (with full Intellisense).Possibilities of use include generating mark-up using models and Editor templates for emails, pdf generation, on-line document delivery, etc.
Your best bet is to construct the link manually using the UrlHelper available in the controller. Having said that, I'm suspicious that there is probably a better way to handle this in a view or partial view, shared or otherwise.