Dont know whether its works for you but you can easily send your action name in a ViewData like ViewData["ActionName"] = "YourActionName" to your view.
What are you trying to accomplish? What do you mean if you're "under an action"?
If you're asking how to get the name of the current controller and action from the View, you can do that with something like this
Dim action as String = ViewContext.Controller.ValueProvider("action").RawValue
Dim controller as String = ViewContext.Controller.ValueProvider("controller").RawValue
If you're asking how to get the name of the current controller and action from the View, you can do that with something like this
But I'm getting this error "Reference to a non-shared member requires an object reference."
Function Index() As ActionResult Dim action As String = ViewContext.Controller.ValueProvider("action").RawValue
Dim controller As String = ViewContext.Controller.ValueProvider("controller").RawValue
That code was getting the name of the controller and action in the View. To find the names of both in the controller use this:
Function Index() As ActionResult
Dim controllerName As String = CType(Me.ValueProvider("controller").RawValue, String)
Dim actionName As String = CType(Me.ValueProvider("action").RawValue, String)
Return View()
End Function
FYI - Please use Me.RouteData.Values("action") and Me.RouteData.Values("controller") for this instead of going through the ValueProvider. The RouteData contains information on the request / route (including
controller and action), and the value provider contains information used for binding.
Marked as answer by ricka6 on Dec 09, 2009 07:13 PM
imperialx
Member
523 Points
1222 Posts
Get Current Controller in Action
Dec 07, 2009 03:22 AM|LINK
Hello,
I'm in the current controller "HomeController", how do I get it on my action? This yields nothing [:(]
Function Index() As ActionResult
Dim value As ControllerBuilder
value = ControllerBuilder.Current
Return View()
End Function
-imperialx
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: Get Current Controller in Action
Dec 07, 2009 03:51 AM|LINK
You want the instance of the controller? That's the "this" object in C#, or the "Me" object in VB.NET.
imperialx
Member
523 Points
1222 Posts
Re: Get Current Controller in Action
Dec 07, 2009 03:58 AM|LINK
Under the View page, I can specify if I'm within the current controller like,
<%If Not (TypeOf ViewContext.Controller Is ProjectName.HomeController) Then%>
<%Html.RenderAction("ControllerName", "ViewName")%>
<%End If%>
How can I specify if I'm under an Action?
ali62b
Contributor
4750 Points
690 Posts
Re: Get Current Controller in Action
Dec 07, 2009 04:05 AM|LINK
Dont know whether its works for you but you can easily send your action name in a ViewData like ViewData["ActionName"] = "YourActionName" to your view.
hope this helps.
@BlueCoder
Regards
imperialx
Member
523 Points
1222 Posts
Re: Get Current Controller in Action
Dec 07, 2009 04:10 AM|LINK
How about referencing a strongly-typed?
This too yields nothing, [:(]
Dim value As String = Me.ControllerContext.RequestContext.HttpContext.Items("Home")
CodeHobo
All-Star
18669 Points
2648 Posts
Re: Get Current Controller in Action
Dec 07, 2009 04:15 AM|LINK
What are you trying to accomplish? What do you mean if you're "under an action"?
If you're asking how to get the name of the current controller and action from the View, you can do that with something like this
Dim action as String = ViewContext.Controller.ValueProvider("action").RawValue Dim controller as String = ViewContext.Controller.ValueProvider("controller").RawValueBlog | Twitter : @Hattan
imperialx
Member
523 Points
1222 Posts
Re: Get Current Controller in Action
Dec 07, 2009 04:39 AM|LINK
But I'm getting this error "Reference to a non-shared member requires an object reference."
Function Index() As ActionResult
Dim action As String = ViewContext.Controller.ValueProvider("action").RawValue
Dim controller As String = ViewContext.Controller.ValueProvider("controller").RawValue
Return View()
End Function
CodeHobo
All-Star
18669 Points
2648 Posts
Re: Get Current Controller in Action
Dec 07, 2009 05:11 AM|LINK
That code was getting the name of the controller and action in the View. To find the names of both in the controller use this:
Function Index() As ActionResult Dim controllerName As String = CType(Me.ValueProvider("controller").RawValue, String) Dim actionName As String = CType(Me.ValueProvider("action").RawValue, String) Return View() End FunctionBlog | Twitter : @Hattan
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Get Current Controller in Action
Dec 08, 2009 12:02 AM|LINK
FYI - Please use Me.RouteData.Values("action") and Me.RouteData.Values("controller") for this instead of going through the ValueProvider. The RouteData contains information on the request / route (including controller and action), and the value provider contains information used for binding.
imperialx
Member
523 Points
1222 Posts
Re: Get Current Controller in Action
Dec 10, 2009 06:22 AM|LINK
Why do I get this error "Object Type is not reference" when I put it on a constructor?
Public Sub New()
ViewData("ThisIsTheCurrentController") = Me.RouteData.Values("controller")
End Sub