Home > Mobile >  ASP.NET MVC @Url.Action redirects only to controller, not to action
ASP.NET MVC @Url.Action redirects only to controller, not to action

Time:06-09

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 >
            <li role="navigation" ><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 >
        <main role="main" >
            @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>

CodePudding user response:

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

CodePudding user response:

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());
       }

CodePudding user response:

Hi what bootstrap version are you using? Try adding jquery into a head tag, just under bootstrap script

  • Related