0

我有一个这样的搜索 ajax 请求:

  $.ajax({
        type: 'POST',
        data: { FirstName: firstname, LastName: lastname},
        contentType: "application/json; charset=utf-8",
        url: 'GetPeople',
        dataType: 'json',

      }
    });

在 GetPeole 操作中,我可以获得我的参数 (FirstName,LastName)

public virtual JsonResult GetPeople(string FirstName,string LastName)
        {
          ....
        }

如果我改变我的ajax请求

$.ajax({
        type: 'POST',
        data: { FirstName: firstname, LastName: lastname,Age=age},
        contentType: "application/json; charset=utf-8",
        url: 'GetPeople',
        dataType: 'json',

      }
    });

我必须改变我的 GetPeople

 public virtual JsonResult GetPeople(string FirstName,string LastName,int Age)
        {
          ....
        }

我想像这样在 Getpeople 中将我的搜索参数(名字,姓氏,年龄)作为对象

public virtual JsonResult GetPeople(searchParam)
    {
        .....
    }
4

1 回答 1

2

您为参数声明一个类,如下所示:

public class SearchFilters {
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public int Age {get;set;}
}

并像这样在您的控制器中使用它:

public JsonResult GetPeople(SearchFilters filters) {
}

在您的 ajax 帖子中,您需要像这样传递数据:

data: JSON.stringify({ FirstName: firstname, LastName: lastname,Age=age})
于 2013-05-02T12:37:02.827 回答