How to use Url.action with parameters in MVC View

I have a view that has the following script in it:

  <script type='text/javascript'>

      function getURL(controllerName, actionName, routeVals) {
          var result = 0;
          $.ajax({
              data: { actionName: actionName, controllerName: controllerName, routeVals: routeVals },
              async: false,
              type: 'GET',
              url: '@Url.Action("getURL","RequestProcess")',
              success: function (data) {
                  result = data;
              }
          });
          return result;
      }
</script>

Open in new window


Currently the Url.action hard codes the action and controller.  I want to replace the action with the actionName passed into the function and the controller with the controllerName passed into the function.

When I replace "getURL" with actionName and "RequestProcess" with controllerName I receive the following error:

Server Error in '/Apps/Referrals' Application.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0103: The name 'actionName' does not exist in the current context

Source Error:


Line 179:              async: false,
Line 180:              type: 'GET',
Line 181:              url: '@Url.Action(actionName,controllerName)',
Line 182:              success: function (data) {
Line 183:                  //callback

Source File: d:\websites\i21\Apps\Referrals\Views\Home\Index.cshtml    Line: 181 

Open in new window


What do I need to do to pass the actionName and controllerName to the @Url.action call?
dyaroshAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

<script type='text/javascript'>

      function getURL(controllerName, actionName, routeVals) {
          var result = 0;

$.ajax({
    url: '@Url.Action(actionName, controllerName)',
    data: { routeVals: routeVals },
              async: false,
              type: 'GET',
              success: function (data) {
                  result = data;
    }
});

</script>

Open in new window

0
dyaroshAuthor Commented:
As I noted in my post, I have tried that already but it generates an error when I try and generate the page.  Here is the screen shot showing the VS red line under the variables indicating it doesn't like something.
UrlActionError.fw.png
0
Lokesh B RDeveloperCommented:
Hi,

try this

<script type='text/javascript'>

    function getURL(controllerName, actionName, routeVals) {
        var result = 0;
        var Url = actionName + "/" + controllerName;
        $.ajax({
            url: '@Url.Action(Url.ToString())',
            data: { routeVals: routeVals },
            async: false,
            type: 'GET',
            success: function (data) {
                result = data;
            }
        });
    }

</script>

Open in new window

0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

dyaroshAuthor Commented:
I got a Server Error.

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /Home/0

Open in new window


I tried just replacing the hard coded portion as follows:

var URL = "getURL" + "/" + "RequestProcess";

and then in the ajax call:

url: '@Url.Action(Url.ToString())',
0
Lokesh B RDeveloperCommented:
Hi,

What is your ActionName & ControllerName?

In below code "Home" is Controller & "Index" is Action


<script type='text/javascript'>

   
function getURL(controllerName, actionName, routeVals) {
        var result = 0;
        $.ajax({
            url: '@Url.Action("Index","Home")',
            data: { routeVals: routeVals },
            async: false,
            type: 'GET',
            success: function (data) {
                result = data;
            }
        });
    }

</script>

Open in new window

0
louisfrCommented:
Someone found how to get the routes known from javascript : http://jarrettmeyer.com/blog/2012/06/26/getting-asp-dot-net-mvc-routes-into-javascript
0
dyaroshAuthor Commented:
getURL is the action and RequestProcess is the controller
0
Lokesh B RDeveloperCommented:
try this

function getURL(controllerName, actionName, routeVals) {
        var result = 0;
        $.ajax({
           url: '@Url.Action("getURL","RequestProcess")',
            data: { routeVals: routeVals },
            async: false,
            type: 'GET',
            success: function (data) {
                result = data;
            }
        });
    }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dyaroshAuthor Commented:
Thank you.  I was changing the data within the Ajax call when I wanted to set it outside the Ajax call.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.