我想通过以下 jQuery 代码将参数从 View 传递到操作,但我没有成功传递参数。我错过了什么吗?
$('li').click(function () {
employeeID = $(this).attr('id');
window.location.href = '/ApproveWork/GetEmployeeDays/'+employeeID;
});
Controller :
[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
);
}
附注我尝试使用 AJAX jQuery 函数,但这不会返回我在操作中指定的 View 。
最佳答案
你的javascript在 View 页面上吗
如果是这样,请使用 Url.Action 帮助程序以正常方式构建 url
var url = '@Url.Action("Action", "Controller", new {parametername = "REPLACEME"})';
window.location.href = url.Replace('REPLACEME', parameter_value);
已更新以考虑客户端变量 *更新 2:更新了拼写错误*
关于javascript - 使用 jQuery 将参数从 MVC View 传递到操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14398076/