We help IT Professionals succeed at work.
Get Started

How to use Url.action with parameters in MVC View

dyarosh
dyarosh asked
on
5,069 Views
Last Modified: 2015-04-06
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?
Comment
Watch Question
Developer
CERTIFIED EXPERT
Top Expert 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE