admin管理员组

文章数量:1208153

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?

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?

Share Improve this question asked Dec 2, 2015 at 2:48 AMehAMeh 2114 gold badges6 silver badges18 bronze badges 4
  • What is the value of params (use console.log(params);` to check the output) – user3559349 Commented Dec 2, 2015 at 2:51
  • Is your code inside a JS file or inside a CSHTML (razor view) file? – simdrouin 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 – AMeh Commented Dec 2, 2015 at 3:02
  • @simdrouin Inside a razor view file – AMeh Commented Dec 2, 2015 at 3:02
Add a comment  | 

3 Answers 3

Reset to default 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;
}

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;

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;
        }

本文标签: aspnet mvcHow to pass multiple parameters in urlaction in JavaScript in MVCStack Overflow