动态获取 ASP.NET MVC 的路径名
我正在寻找一种解决方案,允许我从调用的partialView 获取父页面路径名。例如,我有一个 Action baa 和一个控制器 boo,因此获取它的 javascript 将是 window.location.pathname,并且它返回 /boo/baa。
问题是我使用了部分视图,因此当我尝试通过 URL 帮助程序检索路径名时,我获得的是 PartialView 视图,而我需要的是调用它的操作的路径名。我想这样做是因为我想根据调用它的父视图的路径名来显示分部视图的某些内容。有没有办法用 ASP.NET MVC 来做到这一点?
预先感谢大家,
I'm looking for a solution that allows me to get the parent page pathname from a partialView invoked. For example I have an Action baa and a controller boo so the javascript to obtain this will be window.location.pathname, and it returns /boo/baa.
The thing is that I use a partial view, so when I try to retrieve the pathname via the URL helper, what I obtain is the PartialView one, and what I need is the pathname for the action that invokes it. I want to do this because I want to show certain things of the partial view depending on the pathname of the parent view that invokes it. Is there a way to do this with ASP.NET MVC?
Thank you all in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在页面的ViewContext上访问RouteValues。这将包含由路由引擎确定的控制器和操作值。然而,您可能需要考虑将确定信息作为模型的一部分,因为这会公开控制器中的逻辑——不过,视图如何处理这些信息完全取决于它。
You can access the RouteValues on the ViewContext on the page. This will contain the controller and action values as determined by the routing engine. You might want to consider making the determining information part of your model, however, as this exposes logic in the controller -- what the view does with this information, though, is completely up to it.