6

I have below javascript function in my MVC application,

function EditProducts(productId, orderId, employeeId, mode) 
{
    mode = "edit";
    debugger;
    var url = '@Url.Action("Index", "Home", new { productId = "__productId__", orderId = "__orderId__", employeeId = "__employeeId__", Mode = "__mode__"})';
    var params = url.replace('__productId__', productId).replace('__orderId__', orderId).replace('__employeeId__', employeeId).replace('__mode__', mode);
    window.location.href = params;
}

But it doesn't work. Here is my controller code by I am not getting any values in below vaiables,

public ActionResult Index(int productId, int orderId, int employeeId, string mode)
{
    return View();
}

Any ideas on how to pass multiple parameters through url.action?

4
  • What is the value of params (use console.log(params);` to check the output) Commented Dec 2, 2015 at 2:51
  • Is your code inside a JS file or inside a CSHTML (razor view) file? Commented Dec 2, 2015 at 2:57
  • @StephenMuecke Here is the response in chrome, params = "/Home?productId=1&orderId=4&employeeId=10&Mode=edit", productId = 1,orderId=4, employeeId=10, mode=edit Commented Dec 2, 2015 at 3:02
  • @simdrouin Inside a razor view file Commented Dec 2, 2015 at 3:02

3 Answers 3

15

Use @Html.Raw to prevent the ampersand from being converted to & inside javascript code

function EditProducts(productId, orderId, employeeId, mode) 
{
    mode = "edit";
    debugger;
    var url = '@Html.Raw(Url.Action("Index", "Home", new { productId = "__productId__", orderId = "__orderId__", employeeId = "__employeeId__", Mode = "__mode__"}))';
    var params = url.replace('__productId__', productId).replace('__orderId__', orderId).replace('__employeeId__', employeeId).replace('__mode__', mode);
    window.location.href = params;
}
Sign up to request clarification or add additional context in comments.

Comments

3

Get the base url to the action method using Url.Action helper method and add the querystring params to that.

This should work fine

$(function(){

  var productId = 23;
  var employeeId = 44;
  var orderId = 34;
  var mode = "tes";

  var url = '@Url.Action("Index", "Post")';
  url += '?productId=' + productId + '&orderId=' + orderId + 
                                            '&employeeId=' + employeeId + '&mode=' + mode;
  window.location.href = url;

Comments

0

Only Converted the numbers to string

 function EditRoles(companyid, roleid) {

            //debugger;

            var url = '@Html.Raw(Url.Action("EditRol", "Rol", new { companyID = "__companyid__", roleID = "__roleID__"}))';
            var params = url.replace('__companyid__', companyid.toString()).replace('__roleID__', roleid.toString());
            window.location.href = params;
        }

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.