Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to include @Html.Actionlink in C# text string?
    primarykey
    data
    text
    <p>While the @Html.Actionlink() helper is very convenient for building <code>&lt;a&gt;</code> 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?</p> <p>For example, if I assign a string variable a value similar to the following:</p> <pre><code>Book.ReadMore = "Click @Html.ActionLink(\"this link\", \"About\", \"Home\") to read more."; </code></pre> <p>And then I try to display it (the literal text plus the link) through my .cshtml page, using code similar to:</p> <pre><code>&lt;p&gt;@Model.ReadMore&lt;/p&gt; </code></pre> <p>All I get in the browser is the whole string exactly as I typed it, including the @Html... etc:</p> <pre><code>Click @Html.ActionLink("this link", "About", "Home") to read more. </code></pre> <p>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 <code>&lt;a&gt;</code> link in this scenario, instead of the string itself?</p> <p><strong>EDIT: More information</strong> - This string is just one item in a collection of about 20-30 strings (displayed using a <code>for loop</code> 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.</p> <p>There must be a "right" way of doing this, which is simple yet maintainable.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload