1

我是 ASP.net MVC 的新手。我正在将 Web 表单中的母版页转换为 MVC 母版页。但是,菜单的 CSS 的设计方式使我无法使用以下语法:

  <li><span><%: Html.ActionLink("Index", "Index", "Home")%></span></li>

我需要以下形式的语法

<a href=""> ..... </a>. 我正在使用 aspx 视图引擎。我使用 Html.ActionLink 语法,我的 CSS 无法正常工作,并且造成了很多混乱。

但是,我需要获得与以前的应用程序一样的外观和感觉。我正在使用 ASP.net MVC2

请帮忙..我不确定这是否可能..

我没有使用以下语法重定向到该页面:

 <a href="/Products/Details/">this prodcut</a>
4

5 回答 5

4

Url.Action 是你的朋友!

使用 Razor 视图引擎:

<a href="@Url.Action("Index", "Home")" style="" class="" whatever="">My link</a>

使用 ASPX 视图引擎:

<a href="<%: Url.Action("Index", "Home") %>" style="" class="" whatever="">My link</a>
于 2013-10-04T13:24:18.600 回答
2

这应该工作

<a href="@Url.Action("Action","Controller")">Text</a>
于 2013-10-04T13:24:23.210 回答
0

To link to an action method in a controller use the following:

<a href="/ControllerName/ActionName">...</a>

So if you want to access the Index method in the Home controller, use:

<a href="/Home/Index">...</a>

You can also add classes using ActionLink, e.g.

Html.ActionLink("LinkText", "Controller", "Action", null, new {@class="css class"})

This will produce a link like the following:

<a href="/Controller/Action" class="css class">LinkText</a>
于 2013-10-04T13:29:28.587 回答
0

即使你使用

  • <%: Html.ActionLink("Index", "Index", "Home")%>
  • 它将最终呈现为 html 页面中的锚链接。这不是标签的问题,它是css的问题,请仔细检查..

    确认在 IE 中使用开发者工具。点击 F12,您可以了解标签在页面中的呈现方式。

    问候, Pavan.g

    于 2013-10-04T13:21:13.397 回答
    0

    所有的CSS都可以工作。

    如果你有这样的 ActionLink:

    Html.ActionLink("Index", "Index", "Home") 
    

    它生成以下 Html:

    <a href="yousite/Home/Index">Index</a>
    

    如果要添加类,只需写如下:

    Html.ActionLink("Index", "Index", "Home", null, new {@class="myclass"})
    

    它生成以下 Html:

    <a class="myclass" href="yousite/Home/Index">Index</a>
    

    在 MVC 中强烈建议使用Html.ActionLink. 基于路由的MVC。如果更改路由,Html.ActionLink会自动更改生成的链接。

    于 2013-10-04T13:23:17.423 回答