将参数从MVC视图传递给使用jQuery的操作

时间:2013-01-18 11:34:21

标签: javascript jquery asp.net-mvc asp.net-mvc-3

我想通过以下jQuery代码将参数从视图传递给动作,但我没有成功传递参数。我错过了什么吗?

$('li').click(function () {  
employeeID = $(this).attr('id');    
window.location.href = '/ApproveWork/GetEmployeeDays/'+employeeID; 
});

控制器:

[HttpGet]
public ActionResult GetEmployeeDays(string userCode)
{
     .....
    return View("GetEmployeeDays", model);
}

从Global.asax路由

public static void RegisterRoutes(RouteCollection routes)
 {
   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
   routes.MapRoute(
          "Default", // Route name
          "{controller}/{action}/{id}", // URL with parameters
           new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );          
  }

P.S。我试图使用AJAX jQuery函数,但这并没有返回我在Action中指定的视图。

3 个答案:

答案 0 :(得分:7)

您的javascript是否在视图页面

如果是这样,请使用Url.Action助手以正常方式构建网址

var url = '@Url.Action("Action", "Controller", new {parametername = "REPLACEME"})';
window.location.href = url.Replace('REPLACEME', parameter_value);

已更新为客户端变量帐户 * 更新2:更新错误 *

答案 1 :(得分:3)

您尝试使用ajax吗?

$.ajax({
  url: '@Url.Action("GetEmployeeDays", "ApproveWork")',
  type: 'GET',
  dataType: 'html',
  contentType: 'application/json; charset=utf-8',
  data: { userCode: $(this).attr('id') }
 })

答案 2 :(得分:0)

您无法使用jquery在testuser的帮助下传递参数。 您可以创建包含url的初始字符串,然后将该字符串传递给@Url.Action

window.location.href