mvc route actionlink url use name instead of id

前端 未结 2 1291
陌清茗
陌清茗 2021-01-06 01:56

I think I already asked this but the solution didn\'t really made sense. Anyway, I have ActionLinks on my views like this:

foreach( var item in Model){
<%         


        
2条回答
  •  离开以前
    2021-01-06 02:57

    Sure, you just have to change your routing rules. Look in your Global.asax.cs file, or in your area registration file, for something like this:

    routes.MapRoute(..., "{controller}/{action}/{id}", ...);
    

    ... and change it to something like this:

    routes.MapRoute(..., "{controller}/{action}/{name}", ...);
    

    Then have your action take the name instead of the ID:

    Html.ActionLink(item.Name, "Details", new {item.Name})
    

提交回复
热议问题