如何在MVC中的JavaScript中传递url.action中的多个参数?

时间:2023-01-23 15:12:39

I have below javascript function in my MVC application,

我的MVC应用程序中有以下javascript函数,

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,

但它不起作用。这是我的控制器代码,我在下面的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?

关于如何通过url.action传递多个参数的任何想法?

3 个解决方案

#1


9  

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

使用@ Html.Raw防止&符号被转换为&在javascript代码中

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

#2


1  

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

使用Url.Action帮助方法获取操作方法的基本URL并将querystring参数添加到该方法。

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;

#3


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

#1


9  

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

使用@ Html.Raw防止&符号被转换为&在javascript代码中

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

#2


1  

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

使用Url.Action帮助方法获取操作方法的基本URL并将querystring参数添加到该方法。

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;

#3


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