开发者

Getting raw text using @Html.ActionLink in Razor / MVC3?

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?


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>


MVCHtmlString.Create should do the trick.


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)


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜