I have a actionresult that look this:
我有一个看起来像这样的动作结果:
public ActionResult Comment(int id)
{
var news = re.GetNewsByID(id);
NewsViewModel model = new NewsViewModel();
model.Description = news.Description;
model.Imageurl = news.Image;
model.link = news.Link;
model.PubDate = news.Date;
model.Title = news.Title;
return View(model);
}
and inside the view I have declared:
在我已宣布的视图内:
@Html.ActionLink("Comment", "Comment", new { id = item.Title })
When I click on the actionlink I get to the following url:
当我点击actionlink时,我会看到以下网址:
Home/Comment/403
But I want it to end with the item.title.
但我希望它以item.title结束。
Something like this:
像这样的东西:
Home/Comment/403/What-ever-the-item-title-contains
How can I achieve that?
我怎样才能做到这一点?
2 个解决方案
#1
1
I would 1st map that route in my Routes Collection:
我会在我的Routes Collection中首先映射该路线:
routes.MapRoute(
name: "Title",
url: "{controller}/{action}/{id}/{title}",
defaults: new { controller = "Home", action = "Index", id = 0, title = UrlParameter.Optional }
);
Make sure this route in mapped UNDER your default route. Once you have this then just do:
确保此路由映射在您的默认路由下。一旦你有了这个,那么就这样做:
@Html.ActionLink("Comment", "Comment", new { id = item.Id, title = item.Title })
For that matter you might just want to make your default route look like the route I made above. There is no need for 2 routes since both ID and Title are optional.
就此而言,您可能只想让您的默认路线看起来像我上面做的路线。不需要2条路线,因为ID和Title都是可选的。
#2
0
I never knew how to do it from Html.ActionLink
, I always used Url.Action
like so:
我从来不知道如何从Html.ActionLink中做到这一点,我总是像这样使用Url.Action:
<a href="@Url.Action("Comment", new { id = item.Title })/@theTermToAddHere">Comment</a>
Note the "/" in the href is the markup (not a part of the Razor code), and then I append the additional term.
注意href中的“/”是标记(不是Razor代码的一部分),然后我追加附加术语。
#1
1
I would 1st map that route in my Routes Collection:
我会在我的Routes Collection中首先映射该路线:
routes.MapRoute(
name: "Title",
url: "{controller}/{action}/{id}/{title}",
defaults: new { controller = "Home", action = "Index", id = 0, title = UrlParameter.Optional }
);
Make sure this route in mapped UNDER your default route. Once you have this then just do:
确保此路由映射在您的默认路由下。一旦你有了这个,那么就这样做:
@Html.ActionLink("Comment", "Comment", new { id = item.Id, title = item.Title })
For that matter you might just want to make your default route look like the route I made above. There is no need for 2 routes since both ID and Title are optional.
就此而言,您可能只想让您的默认路线看起来像我上面做的路线。不需要2条路线,因为ID和Title都是可选的。
#2
0
I never knew how to do it from Html.ActionLink
, I always used Url.Action
like so:
我从来不知道如何从Html.ActionLink中做到这一点,我总是像这样使用Url.Action:
<a href="@Url.Action("Comment", new { id = item.Title })/@theTermToAddHere">Comment</a>
Note the "/" in the href is the markup (not a part of the Razor code), and then I append the additional term.
注意href中的“/”是标记(不是Razor代码的一部分),然后我追加附加术语。