ASP.NET MVC3 URL 和隐藏 Id
我有一堆由 ID 作为键控的实体。在一个屏幕中,我根据 ID 加载“主题”,如下所示: "/Results/Subject/53"
在同一屏幕上,我有一个组合框,用于维护所有主题的列表。用户可以将此组合中的选择更改为另一个主题,然后我使用 ajax 加载该主题的结果。当我执行此操作时,浏览器中的 URL 已过时,因为我现在可能正在查看 /Results/Subject/45
如果用户重新加载,我会重新加载原始文档,而不是新文档。
我要么想更新 URL(听起来很老套),要么想将导航基于除 ID 之外的其他内容作为 URL 的一部分。我该怎么做?如何加载特定项目的控制器而不将 ID 指定为 URL 的一部分。
I have a heap of entities that are keyed by ID. In one screen I load a "Subject" based on the ID like so: "/Results/Subject/53"
On that same screen I have a combo box that maintains a list of all subjects. The user can change the selection in this combo to another subject and I load the results for that subject using ajax. When I do this the URL in the browser is stale as I might be now be looking at /Results/Subject/45
If a user reloads I reload the original document and not the new one.
I would either like to update the URL (which sounds hacky as) or I would like to base my navigation on something else besides the Id as part of the URL. How can I do this? How can I load controllers for specific items without specifying the Id as part of the URL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以通过 HTTP GET 参数传递 ID:
/Results/Subject?id=45
或 HTTP POST 参数。但是,我怀疑这能否回答您的问题。
我的一位同事遇到了类似的问题,他通过更改浏览器 URL 字符串(通过附加特定的#anchor)解决了这个问题。
You can also pass the id via the HTTP GET parameter:
/Results/Subject?id=45
or HTTP POST parameter.However, I doubt that this will answer your question.
A colleague of mine has encountered the similar problem, and he solved it by altering the browser URL string (by attaching a specific #anchor to it).
最简单的方法是当下拉列表发生变化时放弃 ajax,只导航到新的 url (/Results/Subject/NewID)。
如果你想保留ajax,你可以把key放在url的末尾,并加上#,如“/Results/Subject#53”,然后通过Ajax加载它(#53不会传递到服务器) )。这样,当用户更改下拉列表时,您可以导航到“/Results/Subject#NewID”,而无需重新加载页面。
Easiest way would be to ditch the ajax when that drop-down changes, and just navigate to a new url (/Results/Subject/NewID).
If you want to keep the ajax, you could just put the key at the end of the url with a # like "/Results/Subject#53", then load it via Ajax (the #53 will not be passed over to the server). That way, when the user changes the drop-down, you can navigate to "/Results/Subject#NewID" without it reloading the page.