2

我想通过以下 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
        );          
  }

PS 我尝试使用 AJAX jQuery 函数,但这并没有返回我在操作中指定的视图。

4

4 回答 4

9

您的 javascript 在查看页面上吗

如果是这样,请使用 Url.Action 帮助器以正常方式构建 url

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

更新以考虑客户端变量 *更新 2:更新错字*

于 2013-01-18T11:40:01.947 回答
3

你试过ajax吗?

$.ajax({
  url: '@Url.Action("GetEmployeeDays", "ApproveWork")',
  type: 'GET',
  dataType: 'html',
  contentType: 'application/json; charset=utf-8',
  data: { userCode: $(this).attr('id') }
 })
于 2013-01-18T11:55:13.850 回答
2

您能否检查您的 Global.asax 文件中的注册路线?

请检查那里的可选变量名称。

假设您使用以下方法注册路线。

routes.MapRoute( _
            "Default", _
            "{controller}/{action}/{id}", _
            New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _
        )

你必须在下面写下你的动作。

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

或者

[HttpGet]
public ActionResult GetEmployeeDays(string id)
{
     .....
    return View("GetEmployeeDays", model);
}
于 2013-01-18T11:54:40.297 回答
0

您不能在@Url.Actionjquery 的帮助下传递参数。您可以创建包含 url 的初始字符串,然后可以将该字符串传递给window.location.href

$_BaseUrl="Your application url";

<script>
 Window.location.href = $_BaseUrl + '/Admin/Account/SwitchSuperAdminLogin?UserName=' + user + '&Password=' + pass + '&IndustryId=' + indus;
 </script>
于 2017-02-21T11:26:54.363 回答