How to use MVC 3 @Html.ActionLink inside c# code

前端 未结 5 1037
庸人自扰
庸人自扰 2021-01-17 09:59

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 lock         


        
5条回答
  •  旧巷少年郎
    2021-01-17 10:29

    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:

    var link = "Email is... click,  to unlock.";
    

    @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.

提交回复
热议问题