如何创建此操作链接?
我在使用 Preview 5 创建 ActionLink 时遇到问题。我能找到的所有文档都描述了旧的通用版本。
我正在页面 /jobs 上的职位列表上构建链接。 每个作业都有一个 guid,我想构建一个指向 /jobs/details/{guid} 的链接,以便我可以显示有关作业的详细信息。 我的作业控制器有一个索引控制器和一个详细信息控制器。 详细信息控制器需要一个指南。 我已经尝试过了
<%= Html.ActionLink(job.Name, "Details", job.JobId); %>
,但是,这给了我 url“/jobs/details”。 我在这里缺少什么?
已解决,在你的帮助下。
路线(在包罗万象的路线之前添加):
routes.Add(new Route("Jobs/Details/{id}", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(new
{
controller = "Jobs",
action = "Details",
id = new Guid()
}
});
操作链接:
<%= Html.ActionLink(job.Name, "Details", new { id = job.JobId }) %>
html 锚点的结果:
http://localhost:3570/WebsiteAdministration/Details ?id=2db8cee5-3c56-4861-aae9-a34546ee2113
所以,它的路线令人困惑。 我将工作路线定义移至网站管理员之前,现在可以使用了。 显然,我的路线定义很糟糕。 我需要阅读更多示例。
附带说明一下,我的链接没有显示,快速观看也不起作用(无法快速观看匿名类型的表达式),这使得弄清楚这里发生了什么变得更加困难。 事实证明,由于一个非常小的拼写错误,操作链接没有显示:
<% Html.ActionLink(job.Name, "Details", new { id = job.JobId })%>
That’s go to get me again。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试一下:
其中“guid”是路由中参数的实际名称。 这指示路由引擎您要将 job.JobId 属性的值放入路由定义的 guid 参数中。
Give this a shot:
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.
您是否在 Global.asax.cs 文件中定义了处理此问题的路由? 默认路由是{controller}/{action}/{id}。 您正在传递“JobID”,框架不会自动将其映射到“id”。 您需要将其更改为 job.id 或定义一个路由来显式处理这种情况。
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.