ActionLink Includes Current {id} in URL

两盒软妹~` 提交于 2019-12-01 06:36:10

问题


The page currently displayed in my browser is: http://localhost:19255/Object/Browse/1.

A link on this page is created with: @Html.ActionLink("...", "Browse", "Object")

But the generated link is actually: /Object/Browse/1

My understanding of what's happening is that MVC sees my route has an {id} portion, which I did not supply. So it went ahead and included the {id} portion from the current page.

Fair enough, but how do I create a link without it? I tried null, and new { id = null } but neither worked.


回答1:


While neither of the following work:

@Html.ActionLink("...", "Browse", "Object", null)              // Has no effect
@Html.ActionLink("...", "Browse", "Object", new { id = null }) // Error

The following solves the issue:

@Html.ActionLink("...", "Browse", "Object", new { id = "" })   // No ID is passed



回答2:


I believe MVC is pulling this Id from the Model directly since you don't specify it. I've seen this before but I don't have time at the moment to verify it.

Try overwriting @Model.Id before rendering that link to see if that's where it's coming from. Then, I think, you'd be able to stash the Id value off in a new variable in case you need it and set @Model.Id = null. Kind of a hack... of course :)



来源:https://stackoverflow.com/questions/10955466/actionlink-includes-current-id-in-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!