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?
-
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.Shahzad– Shahzad2019年07月03日 06:47:37 +00:00Commented Jul 3, 2019 at 6:47
-
I have edited my question. I'm sorry for the confusion.ikey– ikey2019年07月03日 06:53:18 +00:00Commented Jul 3, 2019 at 6:53
1 Answer 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.
2 Comments
Request
inside the controller action too.