5

Given the following Html.ActionLink:

@Html.ActionLink(Model.dsResults.Tables[0].Rows[i]["title"].ToString(), "ItemLinkClick",
    new { itemListID = @Model.dsResults.Tables[0].Rows[i]["ItemListID"], itemPosNum = i+1 }, ...

Data from the model contains HTML in the title field. However, I am unable to display the HTML encoded values. ie. underlined text shows up with the <u>....</u> around it.

I've tried Html.Raw in the text part of the ActionLink, but no go.

Any suggestions?

ElHaix
  • 12,846
  • 27
  • 115
  • 203
  • 1
    Why are you putting html in your model instead of your view? If you need it to be underlined, add a style or class attibute for that. – Phil Aug 01 '11 at 17:26
  • The data coming back from the model is formatted based on keywords. Thus, formatting is required at that level. – ElHaix Aug 03 '11 at 16:30

5 Answers5

13

If you still want to use a helper to create an action link with raw HTML for the link text then I don't believe you can use Html.ActionLink. However, the answer to this stackoverflow question describes creating a helper which does this.

I would write the link HTML manually though and use the Url.Action helper which creates the URL which Html.ActionLink would have created:

<a href="@Url.Action("ItemLinkClick", new { itemListID = @Model.dsResults.Tables[0].Rows[i]["ItemListID"], itemPosNum = i+1 })">
    @Html.Raw(Model.dsResults.Tables[0].Rows[i]["title"].ToString())
</a>
budi
  • 6,351
  • 10
  • 55
  • 80
dan
  • 2,378
  • 18
  • 17
  • Excellent answer +1. I was looking for a non-Helper based solution throughout the web but all i was getting helper based solutions. You made my day! cheers :) – Steve Johnson May 05 '13 at 17:16
0

You could also use this:

<a class='btn btn-link' 
   href='/Mycontroler/MyAction/" + item.ID + "'
   data-ajax='true' 
   data-ajax-method='Get' 
   data-ajax-mode='InsertionMode.Replace' 
   data-ajax-update='#Mymodal'>My Comments</a>
RichardBernards
  • 3,146
  • 1
  • 22
  • 30
Arash Yazdani
  • 302
  • 2
  • 12
0

MVCHtmlString.Create should do the trick.

Chris Kooken
  • 32,730
  • 15
  • 85
  • 123
0

Using the actionlink below you do not need to pass html in the model. Let the css class or inline style determine how the href is decorated.

@Html.ActionLink(Model.dsResults.Tables[0].Rows[i]["title"], "ItemLinkClick", "Controller", new { @class = "underline", style="text-decoration: underline" }, null)
Phil
  • 4,134
  • 4
  • 23
  • 40
  • The decoration is not for the entire string, only part of it, so I don't think this will do. – ElHaix Aug 03 '11 at 16:29
0

those are the cases that you should take the other path

@{
    string title = Model.dsResults.Tables[0].Rows[i]["title"].ToString(),
           aHref = String.Format("/ItemLinkClick/itemListID={0}&itemPosNum={1}...", 
                       Model.dsResults.Tables[0].Rows[i]["ItemListID"],
                       i+1);
}

<a href="@aHref" class="whatever">@Html.Raw(title)</a>

Remember that Razor helpers, help you, but you can still do things in the HTML way.

balexandre
  • 73,608
  • 45
  • 233
  • 342