Html.Actionlink与ID [英] Html.Actionlink with ID
问题描述
我有这样的code:
@Html.ActionLink("Back To Blog","Blog","Home", new {Model.BlogId}, null)
这code是产生此HTML:
That code is generating this HTML:
<a href="/Home/Blog?BlogId=1">Back To Blog</a>
但我想它生成的是这样的:
<a href="/Home/Blog/1">Back To Blog</a>
我该如何解决这个问题?
How do I fix this?
PS - 我看到了SO这个帖子这似乎表明我做了正确的方式,但我没有得到我想要的结果。我使用MVC4,最新的版本。
PS - I saw this post on SO which seems to indicate I'm doing it the right way, but I am not getting the results I want. I am using MVC4, latest version.
推荐答案
我怀疑你没有配置为路线主页/博客/ {BlogId}
。因此,如果你希望能够写这个(如你在code显示):
I suspect you don't have a route configured for Home/Blog/{BlogId}
. So if you want to be able to write this (as what you show in your code):
@Html.ActionLink("Back To Blog","Blog","Home", new { Model.BlogId}, null)
您需要有一个这样的路线:
You need to have a route like this:
routes.MapRoute(
name: "DefaultBlog",
url: "{controller}/{action}/{blogid}",
defaults: new { controller = "Home", action = "Blog",
blogid = UrlParameter.Optional }
);
注意被命名为URL的第三部分 blogid
。但是,如果你不想配置其它路由那么你可以重写你的链接为:
Notice the third part of the url which is named blogid
. But if you don't want to configure another route then you can just rewrite your link as:
@Html.ActionLink("Back To Blog","Blog","Home", new { id = Model.BlogId}, null)
注意 ID
的参数。
这篇关于Html.Actionlink与ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!