ASP.NET I'm creating movie website in C#. My issue is making top navbar buttons target/open in new tab

147 Views Asked by At

ASP.NET

I am trying to create a movie website in C#. I am having issues with making the nav bar buttons at the top target and open in a new tab. This is what my nav bar code looks like. Where am I going wrong?

Button names: Home - Top Movies - All Movies - All

<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("IMDB Dataset", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>

-navbar code-
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">


<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Top Movies", "Top Movies", "Home")</li>
<li>@Html.ActionLink("All Movies", "All Movies", "Home")</li>
<li>@Html.ActionLink("All Directors", "All Directors", "Home")</li>

</ul>

<ul class="navbar-link">
<li></li>
</ul>
</div>
</div>
</div>
2

There are 2 best solutions below

11
On

You can use an ActionLink overload:

@Html.ActionLink("Top Movies", "Top Movies", "Home")

Should be

@Html.ActionLink("Top Movies", "TopMovies", "Home", null, new {target="_blank"})

TopMovies being your Action method. Make sure your Action do not have spaces, notice your "Top Movies" vs my "TopMovies", they must match your controller action method names.

0
On

First, make sure that you have created the corresponding Controller actions (TopMovies) and subsequent views. for example: Your link should look like the following:

<li>@Html.ActionLink("Top Movies", "TopMovies", "Movies", null, new {target="_blank"})</li></a>/li>

In this example your Action would be TopMovies as specified in your Movies controller. Then the result would be displayed in a view as specified in the action.

You can see more info in the following tutorial:

https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-5.0&tabs=visual-studio