Question

I have a SQL query where I want to pass controller name as one of the where clause compare parameter. And there are many controllers within the application. Depending on the controller name there will be different result of query to be executed. So how can I pass name of the controller to some third controller.

Using this @ViewContext.RouteData.Values["controller"], I can get name of controller but within the view of that controller only. So how can this be achieved in another controller action method.

No correct solution

OTHER TIPS

There are several ways how to pass value to an action method in ASP MVC. It depends on the type of request.

Example for GET request

// navigation to action
@Html.ActionLink("link","myAction","otherCon", new {controllerNamePar = ViewContext.RouteData.Values["controller"]}

// render result of action into current view
@{Html.RenderAction("myAction","otherCon", new {controllerNamePar = ViewContext.RouteData.Values["controller"]}}

controllerNamePar will be sent as the query string in the url.

Example for POST request

@using (Html.BeginForm("MyAction", "OtherCon", FormMethod.Post))
{
    @Html.Hidden("controllerNamePar", ViewContext.RouteData.Values["controller"])
    <input type="submit" value="OK" />
}

controllerNamePar will be sent as the part of html form.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top