在视图中获取当前控制器 [英] Get current controller in view
问题描述
我有一个 View - _Edit
,它位于 News M/V/C
.
I have a View - _Edit
which lives in News M/V/C
.
我通过 CategoryController
重用 V/M
作为:
I reuse the V/M
via the CategoryController
as:
return PartialView("/Views/News/_Edit.cshtml", model);
如何在 View - _Edit
中提醒控制器名称?
How from within the View - _Edit
can I alert the controller name?
当我:
alert('@ViewContext. RouteData.Values["controller"].ToString()');
值为:News
但是,URL 是:/Category/foobar
The Value is: News
However, the URL is: /Category/foobar
有没有办法让 'Category'
值提醒?谢谢
Is there a way to get the value 'Category'
to alert? thanks
推荐答案
我已经把这个放在我的部分视图中:
I have put this in my partial view:
@HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()
在您描述的同类情况下,它显示 URL 中描述的控制器(您的类别,我的产品),而不是局部视图的实际位置.
in the same kind of situation you describe, and it shows the controller described in the URL (Category for you, Product for me), instead of the actual location of the partial view.
所以改用这个警报:
alert('@HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()');
这篇关于在视图中获取当前控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!