0

我有一个看起来像这样的actionresult:

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);
}

在我声明的视图中:

@Html.ActionLink("Comment", "Comment", new { id = item.Title })

当我单击操作链接时,我会访问以下网址:

Home/Comment/403

但我希望它以 item.title 结尾。

像这样的东西:

Home/Comment/403/What-ever-the-item-title-contains

我怎样才能做到这一点?

4

2 回答 2

1

我会在我的路线集合中第一次映射该路线:

routes.MapRoute(
  name: "Title",
  url: "{controller}/{action}/{id}/{title}",
  defaults: new { controller = "Home", action = "Index", id = 0, title = UrlParameter.Optional }
        );

确保这条路线在您的默认路线下映射。一旦你有了这个,那就做:

@Html.ActionLink("Comment", "Comment", new { id = item.Id, title = item.Title })

就此而言,您可能只想让您的默认路线看起来像我上面制作的路线。不需要 2 条路线,因为 ID 和 Title 都是可选的。

于 2013-05-15T19:22:10.760 回答
0

我从来不知道怎么做Html.ActionLink,我总是Url.Action这样使用:

<a href="@Url.Action("Comment", new { id = item.Title })/@theTermToAddHere">Comment</a>

请注意,href 中的“/”是标记(不是 Razor 代码的一部分),然后我附加了附加术语。

于 2013-05-15T18:15:12.953 回答