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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14398076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:38:45  来源:igfitidea点击:

Pass parameter from MVC view to an action with jQuery

javascriptjqueryasp.net-mvcasp.net-mvc-3

提问by pisi

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. Am I missing something?

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

$('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
        );          
  }

P.S. 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 中指定的视图。

回答by NinjaNye

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:更新错字*

回答by Pablo Claus

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') }
 })

回答by alok_dida

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

回答by Rohit Grover

You cannot pass parameter with the help of @Url.Actionwith jquery. You can create initial string containing url and then you can pass that string to window.location.href

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

$_BaseUrl="Your application url";

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