1

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>
aenglish
  • 11
  • 4
  • it seems like you are trying to put the horse before the cart. Make Sure that you have the Views that you are wanting to display in your tab. If you have static views then you can do away without @Html.ActionLink() which requires a 'Controller' and 'Action'. If this doesn't make any sense then you need to start with a tutorial like the one I provided in my answer. – DaniDev Dec 07 '20 at 00:27

2 Answers2

0

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.

mxmissile
  • 11,464
  • 3
  • 53
  • 79
  • Thats not working. It doesn't pull up a new window or anything? – aenglish Dec 04 '20 at 19:50
  • Post the rendered html link tag. – mxmissile Dec 04 '20 at 20:31
  • I'm sorry this probably sounds ridiculous but what is that? – aenglish Dec 04 '20 at 20:37
  • Post the HTML code that was generated by the ActionLink() call, right click on the link in your browser and select Inspect, should show you the generated html. – mxmissile Dec 04 '20 at 20:39
  • okay I adjusted the question to only include what was generated – aenglish Dec 04 '20 at 20:53
  • That's not rendered HTML, it should not have any of your @Html.ActionLink calls. Another way is to view the page source in your browser, will show the rendered html. – mxmissile Dec 04 '20 at 21:26
  • The use of actionilst() @Html.ActionLink("All Directors", "All Directors", "Home"), requires a Controller name and Action name. the way you are using it indicates that you do not have those in place. Please read my answer – DaniDev Dec 04 '20 at 22:48
  • @DaniDev huh? `"TopMovies", "Home"` TopMovies being the action, Home being the controller. OP has TopMovies a member of HomeController by the code posted. – mxmissile Dec 05 '20 at 16:14
  • Just trying to make sure @aenglish correctly understands what controllers and actions are and that he has that code correctly in place. – DaniDev Dec 05 '20 at 22:17
  • No I am still very confused @DaniDev – aenglish Dec 05 '20 at 22:57
  • follow the tutorial I posted in my answer – DaniDev Dec 06 '20 at 01:42
0

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

DaniDev
  • 2,471
  • 2
  • 22
  • 29