0

I am retrieving controller name from a javascript function as a string, and need to pass controller name in Url.Action. Any suggestion would be helpful.

var controllerName = "MyController";
var id=1;
@Url.Action("LoadAction","'"+ controllerName +"'")?id=' + id
// Here i am unable to pass controllerName, defined using javascript.

Thanks.

2
  • The C# code is executed on the server, the JavaScript code is executed on the client. What you want to do is not possible this way. You should explain what your actual goal is so that people can propose a solution. Commented Jul 24, 2019 at 15:56
  • See this. I think that will help you. Commented Jul 24, 2019 at 16:04

1 Answer 1

0

If you want to send data from a js script to a C# controller, then you can use a Jquery-ajax call instead of @Url.Action, if I'm not mistaken, you can't even use @Url.Action on a js source code.

const sendId = () => {
    const controllerName = 'MyController';
    const id = 1;
    $.ajax({
          contentType: 'application/json',
          data: { myId: id },
          url: `${controllerName}/methodName`, //template string
          type: 'POST',
          success: function (data) {
             //...
          },
          failed: function () {
            //...
          }
     });
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.