skip to Main Content

I am learning the basis of HTML, Bootstrap & JavaScript. I want to use bootstrap nav class for navigation. The first problem: bootstrap doesn’t work in shared view _Layout.cshtml and buttons look like:

enter image description here

But the content of index.cshtml looks OK.
The second problem: when I click on Messages, it redirects me only to controller ../User, not to its action ../User/Index. I have no idea why it does not work.

Below my _Layout.cshtml code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta name="viewport" content="width=device-width" />
    <link href="~/lib/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
    <script src="~/lib/twitter-bootstrap/js/bootstrap.js"></script>
    <title>@ViewData["Title"] - title</title>
    <link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
    <header>
        <ul class="nav nav-tabs">
            <li role="navigation" class="active"><a href="#">Home</a></li>
            <li role="navigation"><a href="#">Profile</a></li>
            <li role="navigation"><a href="@Url.Action("Index", "User")">Messages</a></li>
        </ul>
    </header>
    <div class="container">
        <main role="main" class="pb-3">
            @RenderBody()
        </main>
    </div>
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>
    @RenderSection("Scripts", required: false)
</body>
</html>

2

Answers


  1. Chosen as BEST ANSWER

    Everything works fine when I manually go to ../User/Index view. Here is the part of UserController.cs class:

    public class UserController : Controller
       {
           private readonly DietDbContext _dbContext;
           private readonly IMapper _mapper;
    
           public UserController(DietDbContext dbContext, IMapper mapper)
           {
               _dbContext = dbContext;
               _mapper = mapper;
           }
    
           public ActionResult Index()
           {
               return View(_dbContext.Users.ToList());
           }
    

  2. For first problem, Try to change from

    <link href="~/lib/bootstrap/dist/css/bootstrap.css" rel="stylesheet" /> 
    

    To:

    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    

    For second problem, try to change from @Url.Action("Index", "User") to @Url.Action("User")

    You have to make sure that your controller naming is correct. Otherwise, we may need your UserController sample code for further assist

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search