Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call action method with parameters MVC 4 javascript

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!

like image 929
dansv130 Avatar asked Jul 17 '13 14:07

dansv130


People also ask

What is action parameters in MVC?

Action Method Parameters are most important in MVC. If you want to handle post request in action methods; MVC framework provided types of Action Methods Parameters. Action Method Parameters. We can organize the action methods for GET and POST requests separately.

How do you pass an object from one action to another action in MVC?

You can use TempData to pass the object.


2 Answers

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})
like image 163
simonlchilds Avatar answered Oct 13 '22 00:10

simonlchilds


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.

like image 40
dansv130 Avatar answered Oct 13 '22 00:10

dansv130