How can I create this action link?

萝らか妹 提交于 2019-12-24 11:30:14

问题


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:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/150845/how-can-i-create-this-action-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!