-2

I created this:

<a href="~/Areas/Admin/Views/Event/Index.cshtml"><span>Events:</span>&nbsp;<span class="badge">@ViewBag.Count</span></a>

to count the Number of tables. When I try to navigate to that reference: ~/Areas/Admin/Views/Event/Index.cshtml i get the following:

HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

I have verified it, I have changed it to: @Html.ActionLink("Event", "Index") to: @Url.View("Event", "Index") and many more, I've viewed carefully the path, I've removed the Authorize from the controller and searched on internet but nothing.

Can anybody give me some kind of advice?

Timigen
  • 1,055
  • 1
  • 17
  • 33
Jon A
  • 137
  • 2
  • 11
  • Have you tried it without ~ – PersyJack Feb 17 '17 at 14:04
  • 1
    You don't link directly to views. You link to actions. Remember that you're using MVC. –  Feb 17 '17 at 14:05
  • If a take the path off it shows the same thing! – Jon A Feb 17 '17 at 14:05
  • You should use @Url.Action("ActionName", "ControllerName") in this case – Esko Feb 17 '17 at 14:07
  • @Amy please view the question, I allready did that: "@html.ActionLink("Event", "Index")". – Jon A Feb 17 '17 at 14:07
  • @Esko it's the same. display error. – Jon A Feb 17 '17 at 14:08
  • @JonA What is the output html now? – Esko Feb 17 '17 at 14:09
  • You cannot link cshtml directly. Must be ~/Controller/Action – Yuri Morales Feb 17 '17 at 14:15
  • @Yuri something like this: "~/Event/Index" I have allready tryed without success. – Jon A Feb 17 '17 at 14:16
  • You need to give us more to go on than "it gives me an error". Explain what its actually doing, we aren't going to continue shooting in the dark. –  Feb 17 '17 at 14:16
  • Here Amy this is the error that I get: "HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly." it was in the original question posted, so sorry about that. – Jon A Feb 17 '17 at 14:18
  • What's the name of the controller and the action you are trying? – Yuri Morales Feb 17 '17 at 14:25
  • What about this: `@Html.ActionLink("url text", "Index", "Event", new { area = "Admin" }, null);` – Grizzly Feb 17 '17 at 14:29
  • Why isn't my answer the accepted answer? I posted the correct solution. The answer you have as accepted will give a run-time error due to an incorrect overload of Html.ActionLink – Grizzly Feb 17 '17 at 14:40

3 Answers3

1

Your action and controller are backwards. The first argument is also the label. See the documentation:

public static MvcHtmlString ActionLink( this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName

Your current code is:

@Html.ActionLink("Event", "Index")

However, you have the order wrong and you're missing the label. Assuming your controller is EventController, this would be

@Html.ActionLink("the link label", "Index", "Event")
  • Sorry Amy, but it's the same it shows the same error. – Jon A Feb 17 '17 at 14:14
  • This is very frustrating! – Jon A Feb 17 '17 at 14:14
  • What does it actually render as? "It doesn't work" doesn't help me understand why it doesn't work. –  Feb 17 '17 at 14:15
  • For your last post with the link label it display's the following error: – Jon A Feb 17 '17 at 14:23
  • An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. – Jon A Feb 17 '17 at 14:24
  • **What does the link actually render as?** Please give us more to go on. Your updated error message indicates the link worked and you're now having a problem server-side. –  Feb 17 '17 at 14:26
  • @JonA When you run your application.. right-click the link and hit Inspect.. or View Source even.. and post what that link is being rendered as. – Grizzly Feb 17 '17 at 14:26
  • @JonA try: `@Html.ActionLink("url text", "Index", "Event", new { area = "Admin" }, null);` – Grizzly Feb 17 '17 at 14:30
  • @JonA I added my answer, please mark as accepted so that this question can be seen as answered – Grizzly Feb 17 '17 at 14:34
1

Assuming that the controller inside the area "Admin" is called event, the Razor code for generating the correct link will be:

@Html.ActionLink("url text", "Index", "Event", new { area = "Admin" });
// Note: You might have problem with the "badge" class

using a classic html url:

<a href='@Url.Action("Index", "Event", new { area = "Admin" })'>
     <span>Events:</span>&nbsp;<span class="badge">@ViewBag.Count</span>
</a>
Ionut Ungureanu
  • 1,020
  • 1
  • 13
  • 16
  • Do you have a controller class called "EventController" in your "Admin" Area? Do you have an action called "Index" in that controller? – Ionut Ungureanu Feb 17 '17 at 14:27
  • The first action link you posted is incorrect.. there is no overload that accepts a `string,string,string,RouteValueDictionary`.. you need to add html attributes.. like so: `@Html.ActionLink("url text", "Index", "Event", new { area = "Admin" }, null);` – Grizzly Feb 17 '17 at 14:28
1

If you are using Areas. Then you need to use the correct form of the Html.ActionLink overload.

Here is what you need:

@Html.ActionLink("url text", "Index", "Event", new { area = "Admin" }, null);

Hope this helps!

Grizzly
  • 5,873
  • 8
  • 56
  • 109