2

When coding a MVC 5 view, how do I redirect to a specific action result specifying the controller, action result and a parameter?

I am wanting to redirect to this action result in a Dashboard controller:

public ActionResult Location(long id)
{

}

I am wanting to do this in a javascript function if possible.

Here is my javascript code:

function MapMarkerClick(locationId)
{
    Response.Redirect(Url.Action("Location", "Dashboard", new { id == locationId }));
}

This is the error that I am getting in the browser console:

Uncaught SyntaxError: Unexpected token ==

Simon
  • 7,991
  • 21
  • 83
  • 163
  • `new { id == locationId }` is not JavaScript. – Henrik Andersson Jun 03 '15 at 05:24
  • You either want to redirect to another action in MVC : http://stackoverflow.com/questions/4909670/asp-net-mvc-3-redirect-to-another-action or you want Razor to provide an URL for Javascript document.location : http://stackoverflow.com/questions/4332253/embedding-javascript-variable-within-razor-syntax – Leon Jun 03 '15 at 05:31

1 Answers1

0

Solution 1 : use cshtml in javascript

    function MapMarkerClick(locationId)
    {
        var url = @Url.Action("Location", "Controller", new { Id= locationId });
        $.ajax({
                url: url ,    
                success: function(){
                    alert('success');      
                }
        });
    }

Solution 2 : Use ActionLink which would render itself as URLs.

@Html.ActionLink("Text", "Location", "Controller", new { Id= locationId })

Solution 3 : using Ajax directly

$.post('Controller', function(data) {

  });
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112