jQuery将params传递给MVC Controller。为什么我的参数空了?

时间:2010-09-30 17:21:47

标签: c# jquery asp.net-mvc

我正在将Web窗体应用程序转换为MVC。

我有一个调用Controller操作方法的js方法(下面)

function GetUsers(pageNum) { var sortTh = $('thead .sort', document.getElementById('usersTable')); var sortCol = sortTh.find('a').attr('rel');

// { } is declarative shorthand for new Object().
var userRequest = {};
userRequest.pageNum = pageNum;
userRequest.pageSize = 10; //records per page
userRequest.sortType = (sortTh.hasClass('ascending')) ? 'ascending' : 'descending';
userRequest.sortColumn = sortCol;
//alert(userRequest.sortType + " - " + sortCol);
// Create a data transfer object (DTO) with the proper structure.
var DTO = { 'userRequest': userRequest };

//Ajax
var urlMethod = "/Users/GetUsers";  //"../WebServices/ws_Users.asmx/GetUsers";
var jsonData = JSON.stringify(DTO);
SendAjax(urlMethod, jsonData, ReturnGetUsers, 'get');

// { } is declarative shorthand for new Object(). var userRequest = {}; userRequest.pageNum = pageNum; userRequest.pageSize = 10; //records per page userRequest.sortType = (sortTh.hasClass('ascending')) ? 'ascending' : 'descending'; userRequest.sortColumn = sortCol; //alert(userRequest.sortType + " - " + sortCol); // Create a data transfer object (DTO) with the proper structure. var DTO = { 'userRequest': userRequest }; //Ajax var urlMethod = "/Users/GetUsers"; //"../WebServices/ws_Users.asmx/GetUsers"; var jsonData = JSON.stringify(DTO); SendAjax(urlMethod, jsonData, ReturnGetUsers, 'get');

此代码有效,您甚至可以在Firebug中看到帖子。

{ “userRequest”: “页次”:1, “pageSize的”:10 “sortType”: “降”, “sortColumn”: “LastLoginDate”}}

}

[HttpPost] public ActionResult GetUsers(UsersModels.GetUserRequest userRequest) {

//Talk to Web Service
var wsUsers = new WS_Users();
var userList = wsUsers.GetUsers(userRequest);

return Json(userList);

我在上面设置了一个断点,我的所有参数都是空的?为什么,为什么?

以下是模型:

//Talk to Web Service var wsUsers = new WS_Users(); var userList = wsUsers.GetUsers(userRequest); return Json(userList);

帮助!

3 个答案:

答案 0 :(得分:1)

我不确定,但我想知道GetUserRequest的成员是否需要是公共属性而不是公共字段?

答案 1 :(得分:1)

SendAjax(urlMethod, jsonData, ReturnGetUsers, 'get');

我不知道SendAjax代码中发生了什么,但“get”看起来应该是对我的“帖子”

答案 2 :(得分:0)

你的GetUserRequest模型应该是这样的

public class GetUserRequest { 
  public int pageNum {get;set;}
  public int pageSize {get;set;}
  public string sortType {get;set;}
  public string sortColumn{ get;set;}
}