简体   繁体   English

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

[英]Pass parameter from MVC view to an action with jQuery

I want to pass a parameter from a view to an action through the following jQuery code, but I don't succeed to pass the parameter. 我想通过以下jQuery代码将参数从视图传递给动作,但我没有成功传递参数。 Am I missing something? 我错过了什么吗?

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

Controller: 控制器:

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

routing from Global.asax 从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 I tried to use AJAX jQuery function, but this doesn't return me the view that I specified in the Action. PS我试图使用AJAX jQuery函数,但这并没有返回我在Action中指定的视图。

Is your javascript on the view page 您的javascript是否在视图页面上

If so use the Url.Action helper to build the url in the normal way 如果是这样,请使用Url.Action帮助程序以正常方式构建URL

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

Updated to account for client side variable * Update 2: updated typo * 更新为客户端变量帐户 * 更新2:更新错误 *

Did you try with ajax? 你尝试过ajax吗?

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

Can you please check your Global.asax file the register routes? 你可以检查你的Global.asax文件的注册路由吗?

Please check the optional variable name over there. 请检查那里的可选变量名称。

Suppose you have register route using following. 假设您使用以下注册路由。

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

You have to write your action below way. 你必须在下面写下你的行动。

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

or 要么

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

You cannot pass parameter with the help of @Url.Action with jquery. 你不能在@Url.Action的帮助下用jquery传递参数。 You can create initial string containing url and then you can pass that string to window.location.href 您可以创建包含url的初始字符串,然后将该字符串传递给window.location.href

$_BaseUrl="Your application url";

<script>
 Window.location.href = $_BaseUrl + '/Admin/Account/SwitchSuperAdminLogin?UserName=' + user + '&Password=' + pass + '&IndustryId=' + indus;
 </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM