3

Please i am having an issue with one of my controllers for hours now. I have succesfully use similar metothos through out the project. However, for some reason this controller's paramterer is always null. Below is my controller.

[Authorize(Roles = "Employer")]
public class EmployerController : Controller
{
  [HttpGet]
  public ActionResult Index(long? id)
  {

  }

}

My Action Link is below

 <p>@Html.ActionLink("View jobs", "Index", "Employer", new { id = userid})</p>

I used a break point and confirmed that my userid is not null.

Below is my routing.

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
               name: null,
               url: "{controller}/Page{page}",
               defaults: new { Controller = "Home", action = "Index" }
               );
            routes.MapRoute(
                name: null,
                url: "{Controller}",
                defaults: new { Controller = "Home", action = "Index" }
                );

          routes.MapRoute(
          name: null,
          url: "{controller}/{action}/{id}/{title}",
          defaults: new
          {
              controller = "Home",
              action = "Index",
              title=UrlParameter.Optional,
              id = UrlParameter.Optional
          }
          );
            routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new
            {
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional
            }
            );
            routes.MapRoute(null, "{controller}/{action}");
        }
    }

I am expecting my action to be served by route four .

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new
                {
                    controller = "Home",
                    action = "Index",
                    id = UrlParameter.Optional
                }

Previously my controller parameter was employerId, then i though that may be the cause of my error because i use id in route. I changed to id and yet still is passing empty. My url is returning something like.

Employer?Length=8

Where do the LEnghth came from ? Please where do i go wrong? Any help would be appreciated.

Nuru Salihu
  • 4,756
  • 17
  • 65
  • 116
  • 1
    You are using the wrong overload of the `ActionLink`. There are plenty of such questions on the SO, use the search. – Zabavsky Mar 04 '15 at 09:45
  • possible duplicate of [Razor actionlink autogenerating ?length=7 in URL?](http://stackoverflow.com/questions/4357856/razor-actionlink-autogenerating-length-7-in-url) – Zabavsky Mar 04 '15 at 09:47

2 Answers2

5

The problem is that you are using a wrong overload of @Html.ActionLink.

Instead of

<p>@Html.ActionLink("View jobs", "Index", "Employer", new { id = userid })</p>

Try

<p>@Html.ActionLink("View jobs", "Index", "Employer",new { id = userid }, null)</p>

OR

<p>@Html.ActionLink("View jobs", "Index", "Employer",new { id = userid }, new{})</p>
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69
1

You have to use this overload:

@Html.ActionLink("View jobs", "Index", "Employer", new { id = userid}, null)

You are currently using the wrong overload which assumes the controller name as the route values. (It's a sequence of 8 chars)

Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156