How to display text in an MVC view with htmlattrbutes

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

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?

回答1:

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> 


回答2:

I think you can use Url.Action method.

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


回答3:

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> 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!