Nuru Salihu
Nuru Salihu

Reputation: 4928

ActionLink is not passing parameter to the Controller -- asp.Net MVC 4

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.

Upvotes: 3

Views: 3623

Answers (2)

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18873

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>

Upvotes: 5

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38468

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)

Upvotes: 1

Related Questions