如何将CSS类名称添加到ASP.NET MVC 3 Url.Action链接? [英] How do you add a CSS class name to an ASP.NET MVC 3 Url.Action link?

查看:59
本文介绍了如何将CSS类名称添加到ASP.NET MVC 3 Url.Action链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.MVC 3或4(使用Razor)中,如何将CSS类应用于Url.Action()帮助器方法?有可能吗?

In ASP.MVC 3 or 4 (using Razor), how do you apply a CSS Class to a Url.Action() helper method? Is it possible?

所需结果:

<a href="home\index?page=2" class="FOO">BAR</a>

我已经走了这么远:

@Url.Action("Index", "Home", new { page })


更新:谢谢.我犯了一些错误,应该为以后的读者(很可能是我自己)弄清楚.我希望我能给你们一个以上的荣誉.


UPDATE: Thanks all. I made a couple of mistakes that I should clarify for future readers (most likely myself). I wish I could give more than one of you credit.

a)新建{页面}-请勿执行此操作.它将产生不希望的输出.我的意思是:ViewBag.page或ViewBag.pageNum

a) new { page } - do not do this. It will produce undesired output. I meant: ViewBag.page or ViewBag.pageNum

b)你们中的一些人指出,我需要Html.ActionLink()而不是我一直成功使用的Url.Action(),直到我切换到生成完整标签,而不仅仅是URL.

b) As a few of you pointed out, I needed Html.ActionLink() not Url.Action() which I had been using successfully until I switched to generating the full tag, not just the url.

我确认可以满足以下要求:

I confirmed that the following works as desired:

@Html.ActionLink("BAR", "Index", "Home", new { ViewBag.PageNum }, new { @class = "FOO" })

推荐答案

我相信您想使用 .这将允许您添加所需的任何属性.

I believe you want to use an ActionLink instead. This will allow you to add any attributes you want.

@Html.ActionLink("BAR", "Index", "Home", new { page }, new { @class = "FOO" })

输出:

<a href="home/index?page=2" class="FOO">BAR</a>

或者您可以手动"完成

<a href="@Url.Action("Index", "Home", new { page })" class="FOO">BAR</a>

这篇关于如何将CSS类名称添加到ASP.NET MVC 3 Url.Action链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆