John
John

Reputation: 339

How to display text in an MVC view with htmlattrbutes

I have the following code :

@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", 
"Manage", "Account", 
routeValues: null, 
htmlAttributes: new { title = "Manage" })

I just want to display the text (with the correct htmlattribute) (i.e. no link)

Could you help me with the correct syntax please?

Upvotes: 0

Views: 2834

Answers (3)

Russ Cam
Russ Cam

Reputation: 125488

If you want the text with no link i.e. no anchor element, then just use plain HTML

<span title="Manage">Hello @User.Identity.GetUserName()!</span>

Or if you don't want to enclose it within a <span>

<text>Hello @User.Identity.GetUserName()!</text>

But with this you won't get the title attribute since the text is not enclosed within an html tag with which to apply it to.

If you actually want an anchor then you could also use @Url.Action() in conjunction with plain HTML

<a title="Manage" href="@Url.Action("Manage", "Account")">
    Hello @User.Identity.GetUserName()!
</a>

Upvotes: 0

Shyju
Shyju

Reputation: 218732

If i understand correctly,you want to show the text inside your link without an achor tag, but with your html attributes (title attributes)

Try this

<span title="Manage">Hello @User.Identity.GetUserName() !</span>

Upvotes: 1

Krishnraj Rana
Krishnraj Rana

Reputation: 6656

I think you can use Url.Action method.

<a href="@Url.Action("ActionName")">
  <span>"Hello " + User.Identity.GetUserName() + "!"</span>
</a>

Upvotes: 2

Related Questions