传递参数为Dictionary使用jquery getJSON方法

本文关键字:string 使用 jquery 方法 getJSON 参数 Dictionary | 更新日期: 2023-09-27 18:10:31

我试图使用$.getJSON函数传递多个参数。为此,我创建了一个DTO类,定义如下:

public class MyDTO
{
    public string IncludeProperties {get; set;}
    public Dictionary<string,string[]> Predicate {get; set;}
}
这是我的web api函数:
[HttpGet]
[Route("FindAll")]
public IEnumerable <Hotel> FindAll([FromUri] MyDTO predicateDTO) {
  return DataStore.FindAll <Hotel> (predicateDTO.IncludeProperties, PredicateHelper.ConvertStringToLambda <Hotel> (predicateDTO.Predicate));
}
jquery:

var uri = '/api/Hotel/FindAll';
var predicate = {
  "HotelName": ["HOtel", "==", ""],
  "PaymentStatus": ["True", "==", "AND"]
}
var data = {
  IncludeProperties: ["HotelDetails", "HotelDetails.HotelMainPhotos"],
  Predicate: predicate
}
busyIndicatorVisibility(true);
$('#ModalOtel .modal-body').find(".media").remove();
$.getJSON(uri, {
    predicateDTO: data
  }).done( //TODO something)

当我调用我的函数IncludeProperties有项目,但Predicate属性是null。我如何传递Dictionary<string,string[]>作为参数?

传递参数为Dictionary<string,string[]>使用jquery getJSON方法

我将尝试向您展示一个解决方案的想法。

你需要像这样更新你的MyDTO模型:

    public class Predicate
    {
        public string[] HotelName { get; set; } // In javascript can hold: ["HOtel", "==", ""] 
        public string[] PaymentStatus { get; set; }
    }
    public class MyDTO
    {
        public string[] IncludeProperties { get; set; }
        public Predicate predicate { get; set; }
    }

在jQuery函数中,您需要替换$。$.ajax()的getJSON,像这样:

var predicate = {
    "HotelName": ["HOtel", "==", ""],
    "PaymentStatus": ["True", "==", "AND"]
};
var data = {
    IncludeProperties: ["HotelDetails", "HotelDetails.HotelMainPhotos"],
    Predicate: predicate
};
$.ajax(
{
    url: uri,
    type: "POST",
    data: JSON.stringify(data), // To convert javascript objects in json strings.
    dataType: "json",
    contentType: "application/json; charset=utf-8", // To send json objects to the web api controller.
    beforeSend: function ()
    {
        console.log("Wait a minute...");
    },
    success: function (response)
    {
        console.log(response);
    }
});

$.ajax()函数将以以下格式发送json数据:

{
    "IncludeProperties": [
        "HotelDetails",
        "HotelDetails.HotelMainPhotos"
    ],
    "Predicate": {
        "HotelName": [
            "HOtel",
            "==",
            ""
        ],
        "PaymentStatus": [
            "True",
            "==",
            "AND"
        ]
    }
}

在你的web api控制器中,试着改成这样:

public string FindAll(MyDTO predicateDTO) // This contains all model with submodels.
{
     string result = "";
     // You can have access to predicateDTO instance with the current data;
     // predicateDTO.predicate.HotelName...
     return result;
}

我已经改变了我的方法。

 [HttpGet]
 [Route("FindAll")]
 public IEnumerable<Hotel> FindAll(string[] IncludeProperties, string Predicate)
 {
    Dictionary<string,string[]> predicateAsDictionary = JsonConvert.DeserializeObject<Dictionary<string, string[]>>(predicateString)
    return DataStore.FindAll<Hotel>(IncludeProperties, predicateAsDictionary);
 }

getJson方法不会将在jquery中定义的var predicate对象转换为Dictionary,但是当我使用JSON.stringify(predicate)发送此对象时,我可以将该字符串反序列化为Dictionary