1

Im trying to redirect to a Controller from JavaScript using this line of code

 location.href = '/Dashboard/';

It redirects to the Dashboard but on my dashboard view this method is called when the document loads

 $.post("Dashboard/UsersGet", {}, function (dataSet) {
    //do something with the dataset
 }); 

I then get this error.

POST http://localhost:1414/Dashboard/Dashboard/UsersGet 404 (Not Found) 

I can see that dashboard is added to the url twice. How can I redirect to a Controller without this happening?

3 Answers 3

2

Use the Url helper:

@Url.Action("UsersGet", "Dashboard")

Full code:

$.post('@Url.Action("UsersGet", "Dashboard")', {}, function (dataSet) {
    //do something with the dataset
 });    

Routes in Asp .Net MVC don't work like in classic Asp.Net.

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

2 Comments

thanks @gdoron but can you please elaborate why should I use the Url.Action instead of just adding the forward slash :)
@JohandeKlerk. You know how routes work? they can be customized, that Url helper knows how to handle them. In asp.net there wasn't such things. Anyway, in MVC you work with actions and controlles, not URLS. Read more here
1

Try this:

 $.post("/Dashboard/UsersGet", {}, function (dataSet) {
    //do something with the dataset
 }); 

Add / to the url.

Comments

0
 $.post("Dashboard/UsersGet", {}, function (dataSet) {
    //do something with the dataset
 }); 

should be

 $.post("/Dashboard/UsersGet", {}, function (dataSet) {
    //do something with the dataset
 }); 

Without '/' the url which you post will be appended to the current url.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.