6

I'm tired and stupid, but heres my coding problem:

We are using d3.js to draw on a google map element in the MVC4 action method called Live. We have implemented on click for the d3.js element and need to redirect from the javascript to another MVC action method.

the action method "declaration" looks like this:

public ActionResult Visualization(String appId = "", String userId = "")

In javascript we have inside the d3.js function a code snippet that works and looks like this:

.on("click", function (d, i) {
        // just as an example use for the click-event. 
        alert(d.AppName); }

Where d has AppId, and UserId aswell.

What we now want to do is to create a redirect for the click event, calling the action method along with parameters from d. This means when you click the d3.js element you would be redirected to the other page with pre-set parameters.

We have tried things like:

window.location.href = "/StatsLocationController/Visualization/appId=" + d.AppIdentifier + "/userId=" + d.DeviceId

We also tried to use window.location.replace(), but none of it has worked, I guess we haven't figured out the correct syntax for the actionLink, but have a hard time finding examples when googling it. We are thankful for any help we can get!

2 Answers 2

9

Instead of:

window.location.href = Html.ActionLink("Visualization", "StatsLocationController", new{ appId=d.AppId, userId=d.UserId}, new{})

Try:

window.location.href = '@Url.Action("Visualization", "StatsLocationController", new{ appId=d.AppId, userId=d.UserId})'

This is assuming you have your routing set up appropriately?

Edit:

The sort of routing that would make this work is something like:

routes.MapRoute(
    "MyRoute", 
    "{controller}/{action}/{appId}/{userId}", 
    new {
         controller = "Home", 
         action = "Index", 
         appId = UrlParameter.Optional, 
         userId = UrlParameter.Optional 
    });

Second Edit

I've just noticed that you're calling your controller StatsLocationController - Unless your C# class is called StatsLocationControllerController - this is wrong. Asp.Net MVC assumes that for a controller, there will be a class called StatsLocationController and so you should reference your controllers without the Controller part. So in this example your controller should be called StatsLocation which would make the URL look like this:

@Url.Action("Visualization", "StatsLocation", new{ appId=d.AppId, userId=d.UserId})
Sign up to request clarification or add additional context in comments.

6 Comments

Doesn't work, in google chrome console i get the same error as for html.ActionLink. The error is "unexpected identifier", it doesn't seem to understand the new object i create. Visual Studio's intellisense is also telling me that "conditional compilation is turned off" if I mouse over @url.
How would the routing look?
Have you made sure to wrap the call in quotes? If not, you will not be assigning a string to the href property which will make it fall over.
Thanks for the help I will test around with this tomorrow as my work day is ending and I will mark your answer correct if I get it working.
One last thing, @Url.Action is MVC helper for HTML right? I need to create the URL in javascript, as i have to make the redirect call in javascript.
|
2

I forgot I left this without accepting an answer.

simonlchilds have figured out the problem, that we included the Controller in the call.

So the final call looked like:

window.location.href = "/StatsLocation/Visualization/appId=" + d.AppIdentifier + "/userId=" + d.DeviceId

where StatsLocation is the name of the controller.

Such a silly mistake, but at least it had a simple solution and maybe it can help someone.

1 Comment

you answered your own question by saying my answer solved it and then accepted your own answer - seems legit

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.