views:

400

answers:

2

I'm having issues creating an ActionLink using Preview 5. All the docs I can find describe the older generic version.

I'm constructing links on a list of jobs on the page /jobs. Each job has a guid, and I'd like to construct a link to /jobs/details/{guid} so I can show details about the job. My jobs controller has an Index controller and a Details controller. The Details controller takes a guid. I've tried this

<%= Html.ActionLink(job.Name, "Details", job.JobId); %>

However, that gives me the url "/jobs/details". What am I missing here?


Solved, with your help.

Route (added before the catch-all route):

            routes.Add(new Route("Jobs/Details/{id}", new MvcRouteHandler())
      {
       Defaults = new RouteValueDictionary(new
       {
        controller = "Jobs",
        action = "Details",
        id = new Guid()
       })
      });

Action link:

<%= Html.ActionLink(job.Name, "Details", new { id = job.JobId }) %>

Results in the html anchor:

http://localhost:3570/WebsiteAdministration/Details?id=2db8cee5-3c56-4861-aae9-a34546ee2113

So, its confusing routes. I moved my jobs route definition before the website admin and it works now. Obviously, my route definitions SUCK. I need to read more examples.

A side note, my links weren't showing, and quickwatches weren't working (can't quickwatch an expression with an anonymous type), which made it much harder to figure out what was going on here. It turned out the action links weren't showing because of a very minor typo:

<% Html.ActionLink(job.Name, "Details", new { id = job.JobId })%>

That's gonna get me again.

+1  A: 

Have you defined a route to handle this in your Global.asax.cs file? The default route is {controller}/{action}/{id}. You are passing "JobID", which the framework won't map to "id" automatically. You either need to change this to be job.id or define a route to handle this case explicitly.

Kevin Pang
Aah, "magic". Will try this in the morning.
Will
Routes did play a part. Thanks for your help.
Will
+3  A: 

Give this a shot:

<%= Html.ActionLink(job.Name, "Details", new { guid = job.JobId}); %>

Where "guid" is the actual name of the parameter in your route. This instructs the routing engine that you want to place the value of the job.JobId property into the route definition's guid parameter.

LostInTangent