Blackcoil
Blackcoil

Reputation: 173

how to BOLD a fragment of the linkText in an Html.ActionLink?

I have this:

<li><%:Html.ActionLink(user.Email.Replace(Model.SearchString, "<b>" + Model.SearchString + "</b>"), "LoginEdit", "Admin", new { area = "Staff", webUserKey = user.WebUserKey }, null)%>, last login: <%:loginString%></li>

As you can see, I want the portion of the Email string that matches the Model.SearchString to be bolded. I can't figure out the syntax to make this happen, given the context of my code.

Any ideas?

The goal is something like this (assuming user searched for "john"):

<a href="Admin/Login/Edit/456546"><b>john</b>@gmail.com</a>

Upvotes: 1

Views: 1586

Answers (2)

Omar
Omar

Reputation: 40182

Whenever I encounter a situation likes this, I try my best not to embed HTML inside HTML helpers. In addition, I think breaking up your code will help in future maintenance - you're doing a lot in a single function call.

I would prefer doing it this way:

<li> 
    <a href="<%: Url.Action("LoginEdit", "Admin", new { area = "Staff", webUserKey =user.WebUserKey }) %>"> 
        <%: user.Email.Replace(Model.SearchString, "") %>
        <b><%: Model.SearchString %></b>
    </a>
    last login: <%: loginString %>
</li>

It's a few more lines of code, but it makes it much easier to decipher what's going on.

Upvotes: 3

Eric Schoonover
Eric Schoonover

Reputation: 48402

I think the issue is that the output of <%: %> is HTML encoded. So your <b> tag is probably encoded and you see the actual tag in the rendered HTML instead of the bold text.

If user.Email is a trusted value you could skip HTML encoding the output.

<li><%= Html.ActionLink(user.Email.Replace(Model.SearchString, "<b>" + Model.SearchString + "</b>"), "LoginEdit", "Admin", new { area = "Staff", webUserKey = user.WebUserKey }, null)%>, last login: <%:loginString%></li>

For more information see: http://haacked.com/archive/2009/09/25/html-encoding-code-nuggets.aspx

Upvotes: 2

Related Questions