I wanted to combine my CSS and Bootstrap with MVC but I cannot style my links properly. As far as I know I need to use @Html.ActionLink to redirect my webpage. Is there a way to style it?
Everything works while using pure 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>
This is my 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>
I have also tested this on MVC but still does not style:
@Html.ActionLink("Library", "Index", "MyTemplate", null, new { @class = "mx-auto order-0" })
My index.cshtml file looks like this:
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_MasterLayout.cshtml";
}
<h2>Index</h2>
My controller looks like this:
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 file:
<!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 ©2020</p>
</div>
</div>
</footer>
</body>
</html>
Unfortunately styling does not apply. I know I could do @style and style it this way but I wanted all style to be applied in style.css not hardcoding it in each class because it will look bad in my code. Please help
If you want this 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>
Then your MVC should look like this:
<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>
You are warapping your Html.ActionLink
(which renders as an anchor tag) in another a tag. And you cannot have a link inside a link.
Alternativly, you could do this also (notice the href of the a tag is the Razor Url helper):
<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>