【问题标题】:Html.ActionLink cannot be styledHtml.ActionLink 无法设置样式
【发布时间】:2020-11-09 15:57:19
【问题描述】:

我想将我的 CSS 和 Bootstrap 与 MVC 结合起来,但我无法正确设置我的链接样式。据我所知,我需要使用 @Html.ActionLink 来重定向我的网页。有没有办法给它设置样式?

使用纯 CSS 时一切正常:

<div class="mx-auto order-0">
        <a class="navbar-brand mx-auto" href="#">LIBRARY</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
            <span class="navbar-toggler-icon"></span>
        </button>
    </div>

这是我的 MVC:

<div class="mx-auto order-0">
            <a class="navbar-brand mx-auto" href="/Index">
                @Html.ActionLink("Library", "Index", "MyTemplate", null, new { @class = "mx-auto order-0" })
            </a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
                <span class="navbar-toggler-icon"></span>
            </button>
        </div>

我也在 MVC 上对此进行了测试,但仍然没有样式:

@Html.ActionLink("Library", "Index", "MyTemplate", null, new { @class = "mx-auto order-0" })

我的 index.cshtml 文件如下所示:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_MasterLayout.cshtml";
}

<h2>Index</h2>

我的控制器如下所示:

    public class MyTemplateController : Controller
    {
        // GET: MyTemplate
        public ActionResult Index()
        {
            return View("Index");
        }

        public ActionResult About()
        {
            return View("About");
        }

        public ActionResult Contact()
        {
            return View("Contact");
        }
    }
}

HTML 文件:

    <!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <title>@ViewBag.title</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

    <link rel="stylesheet" type="text/css" href="Content/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="Content/style.css">
    <link href="https://use.fontawesome.com/releases/v5.11.2/css/all.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Lato:wght@400;700&display=swap" rel="stylesheet">
    <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
    <script type="text/javascript" src="Scripts/bootstrap.js"></script>
    @RenderSection("head", false)

</head>
<body>
    <nav class="navbar navbar-expand-md navbar-dark" style="background-color: #333;text-align: center;">
        <div class="navbar-collapse collapse w-100 order-1 order-md-0 dual-collapse2">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item dropdown">
                    <a class="nav-link" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Books
                    </a>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                        <a class="dropdown-item" href="#">New books</a>
                        <a class="dropdown-item" href="#">Recommended books</a>
                        <a class="dropdown-item" href="#">Available books</a>

                    </div>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Categories
                    </a>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                        <a class="dropdown-item" href="#">Genres</a>
                        <a class="dropdown-item" href="#">Formats</a>
                    </div>
                </li>
                <li class="nav-item">
                    @Html.ActionLink("About", "About", "MyTemplate")
                    
                    <a class="nav-link" href="/About">About</a>
                </li>
                <li class="nav-item">
                    @Html.ActionLink("Contact", "Contact", "MyTemplate")
                    <a class="nav-link" href="/Contact">Contact</a>
                </li>
            </ul>
        </div>

        @Html.ActionLink("Index", "Index", "MyTemplate")
        @Html.ActionLink("Index", "Index", "MyTemplate", null, new { @class = "mx-auto order-0" })

        <div class="mx-auto order-0">
            <a class="navbar-brand mx-auto" href="/Index">
                @Html.ActionLink("Index", "Index", "MyTemplate", null, new { @class = "mx-auto order-0" })
            </a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
                <span class="navbar-toggler-icon"></span>
            </button>
        </div>
        <div class="navbar-collapse collapse w-100 order-3 dual-collapse2">

            <ul class="navbar-nav ml-auto">

                <form class="form-inline d-none d-lg-block justify-content-center md-form form-sm mt-0" style="padding: 5px">
                    <i class="fas fa-search" aria-hidden="true"></i>
                    <input class="form-control form-control-sm ml-3 w-75" type="text" placeholder="Search"
                           aria-label="Search">
                </form>

                <li class="nav-item">
                    <a class="nav-link" href="#">Sign Up <i class="fa fa-user-plus" aria-hidden="true"></i></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Log In <i class="fa fa-user" aria-hidden="true"></i></a>
                </li>
            </ul>
        </div>
    </nav>

    <main>
        @RenderBody()
    </main>


    <footer>
        <div class="row text-center">
            <div class="col-md-4 col-sm-12 social">
                <a href="https://github.com/V1co?tab=repositories" target="_blank"><i class="fab fa-github"></i></a>
                <a href="https://www.linkedin.com/in/r-pawlowski/" target="_blank"><i class="fab fa-linkedin"></i></a>
                <a href="mailto:rafal.pawlowski1993@gmail.com" target="_blank"><i class="fas fa-envelope-square"></i></a>
                <a href="https://www.facebook.com/v1coprivate" target="_blank"><i class="fab fa-facebook-square"></i></a>
            </div>

            <div class="d-none d-md-inline col-md-4">
                <p class="me"></p>
            </div>
            <div class="d-none d-md-inline col-md-4">
                <p class="me">Rafal Pawlowski &#169;2020</p>
            </div>
        </div>
    </footer>

</body>
</html>

不幸的是,样式不适用。我知道我可以这样做 @style 并以这种方式设置样式,但我希望所有样式都应用在 style.css 中,而不是在每个类中对其进行硬编码,因为它在我的代码中看起来很糟糕。请帮忙

【问题讨论】:

  • 在客户端代码中,链接的HTML结果是什么?
  • 我已经编辑了我的帖子。我已经粘贴了控制器代码和 index.cshtml 代码
  • 好的,但是您的客户端代码中链接的 HTML 是什么?听起来您是在说 class 属性没有被应用,对吗?确认后,确切的 HTML 是什么?
  • 已添加 HTML 文件。刷新页面以查看完整代码。请注意,我每次都以不同的方式添加了 3 次 @Html.ActionLink("Index", "Index", "MyTemplate") 以查看其中一个是否以某种方式起作用,但它们没有样式
  • an @Html.Action 呈现为 &lt;a&gt; 标记...您将标记放入无效的 a 标记中

标签: css asp.net asp.net-mvc model-view-controller styling


【解决方案1】:

如果你想要这个 HTML:

<div class="mx-auto order-0">
        <a class="navbar-brand mx-auto" href="#">LIBRARY</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
            <span class="navbar-toggler-icon"></span>
        </button>
    </div>

那么你的 MVC 应该是这样的:

<div class="mx-auto order-0">
            @Html.ActionLink("Library", "Index", "MyTemplate", null, new { @class = "mx-auto order-0" })
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
                <span class="navbar-toggler-icon"></span>
            </button>
        </div>

您正在将您的 Html.ActionLink (呈现为锚标记)转换为另一个 a 标记。而且您不能在链接中包含链接。

另外,您也可以这样做(注意 a 标签的 href 是 Razor Url 帮助器):

<div class="mx-auto order-0">
        <a class="navbar-brand mx-auto" href="@Url.Action("Index", "MyTemplate")">LIBRARY</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
            <span class="navbar-toggler-icon"></span>
        </button>
    </div>

【讨论】:

  • 第一个解决方案无效,但第二个解决方案有效!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2022-01-06
  • 2017-05-08
  • 2020-12-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多