0

I'm having a problem trying to call a javascript function in a JS file to a controller. I wanted to use the function in the JS file then return the value to a controller who calls that function.

Here is my code: (JS File)

function getQueryString(url) {
    var arrSplit = url.split('?');
    return arrSplit.length > 1 ? url.substring(url.indexOf('?')+1) : '';
}

And I wanted to make a call like this in my controller. (Controller)

private string DoSomething(){
     getQueryString("http://sample.com");
}

Is is this possible? Or if ever do you have any suggestions or any possible workarounds?

2
  • The question is not really clear. What you are exactly trying to achieve, Also, 3 lines of code does not do justice to the problem explanation. Commented Jul 3, 2019 at 6:47
  • I have edited my question. I'm sorry for the confusion. Commented Jul 3, 2019 at 6:53

1 Answer 1

1

First of all, MVC does not work that way. You cannot refer JS methods from your MVC controller. MVC controlled executes on the backend and just formulates your View. Once your HTML view is returned to the client, it executes there, And you JS is on the client-side. If you need to call a method in your JS on the browser side, from a server-side Controller, you need to use SignalR.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. May I know if there is an easier way to convert the javascript function to a controller function? Because I've tried converting it manually but I have a hard time doing it that is why I asked if this is possible.
I am not sure what you are exactly asking, but why can't you write the code on the backend side itself if you need to access that from the controller? If you need the URL, you can get that from the Request inside the controller action too.

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.