0

While the @Html.Actionlink() helper is very convenient for building <a> elements in the .cshtml files, is it possible to construct them inside C# strings, such that they are subsequently rendered correctly in the HTML output?

For example, if I assign a string variable a value similar to the following:

Book.ReadMore = "Click @Html.ActionLink(\"this link\", \"About\", \"Home\") to read more.";

And then I try to display it (the literal text plus the link) through my .cshtml page, using code similar to:

<p>@Model.ReadMore</p>

All I get in the browser is the whole string exactly as I typed it, including the @Html... etc:

Click @Html.ActionLink("this link", "About", "Home") to read more.

Now, for proper SoC, I know that it's not the best of practices to have HTML stuff included in C# code, but is it at all possible to get the proper <a> link in this scenario, instead of the string itself?

EDIT: More information - This string is just one item in a collection of about 20-30 strings (displayed using a for loop in the View). Only a small handful of those items need a link (which is different in each case). Since, as mentioned above, I agree that it's obviously not good practice to use Razor/HTML in Model code, I'm trying to get a simple approach (if possible) which would give me the flexibility of building the link somewhere at the right place, while still yielding the maintainability of MVC SoC.

There must be a "right" way of doing this, which is simple yet maintainable.

2
  • possible duplicate of How to use MVC 3 @Html.ActionLink inside c# code Commented Nov 1, 2013 at 2:18
  • Highest voted (not accepted) answer on the above seems to have what you're looking for Commented Nov 1, 2013 at 2:20

2 Answers 2

1

Your model should not contain HTML, that's a view concern and belongs in view code. Probably you should be using a Razor helper.

In your App_Code folder (create one if you don't have one), add a file, ReadMoreHelpers.cshtml:

@helper ReadMore() {
    <text>Click @Html.ActionLink("this link", "About", "Home") to read more.</text>
}

Then in any view:

@ReadMoreHelpers.ReadMore()

And that will output what you want. If you insist on putting that property in your view, you could do:

Book.ReadMore = "Click " + @Html.ActionLink("this link", "About", "Home").ToHtmlString() + " to read more.";

Then in your view, make sure you use Raw:

@Html.Raw(Book.ReadMore) 

However, I couldn't recommend more strongly that you do not put HTML in your model properties.

Sign up to request clarification or add additional context in comments.

1 Comment

I certainly agree 100% about not having HTML in the model. With the more detailed scenario outlined in my edit to the original post, I'm looking for how to avoid it in fact. I wonder if it's possible (and follows best practices) to achieve clean separation by these steps: (a) In the Model, declare an enum of various tokens, then (b) also in the Model, include those tokens in the string items as required, and finally (c) build a Razor Helper to convert the enum'd tokens into those ActionLinks.
0

I don't think so. The Razor view engine will interpret the ActionLink code during run-time while stuffing it as part of a C# string will be interpreted during compile time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.