我正在尝试使用ajax调用将以下数据传递给我的操作。我不确定我做错了什么,但客户端ID总是0.我在调用中很难编写客户端ID,但我仍然看到零。其他一切都正确通过了。
data: {
"BedID": bedID,
"BedNumber": bedNumber,
"ClientSearchItem": {
"ClientID": 160
},
"BuildingName": buildingName,
"RoomName": roomName,
"RoomNumber": roomNumber,
"SiteName": siteName
},
//我的ajax电话
$.ajax({
url: myUrl
type: "GET",
dataType: "JSON",
data: {
"BedID": bedID, "BedNumber": bedNumber, "ClientSearchItem": { "ClientID": 160 }, "BuildingName": buildingName,
"RoomName": roomName, "RoomNumber": roomNumber, "SiteName": siteName
},
success: function (data) {
// do something here
},
error: function (error) {
alert("An error occured");
}
});
public class SearchClient
{
// parameterless constructor
public SearchClient()
{
}
public int BedID { get; set; }
public int BedNumber { get; set; }
public string BuildingName { get; set;}
//public int ClientID { get; set; }
public ClientSearchItem ClientSearchItem { get; set; }
public bool IsReservation { get; set;}
public string RoomName { get; set;}
public string RoomNumber { get; set; }
public string BedManageOption { get; set; }
public string SiteName { get; set;}
[Required]
public string StartDate { get; set;}
public string StartTime { get; set;}
public DateTimePicker EndDateTime { get; set;}
public string Url { get; set; }
}
public ActionResult _AddClientToBed(SearchClient model)
{
// do something here
}
答案 0 :(得分:0)
我建议使用几点来使代码正常工作。
1)首先添加contentType:" application / json"。
2)其次,将Ajax请求作为" POST"请求,以便Asp.Net MVC可以轻松地将表单内容解析为复杂对象。 (虽然它也应该从查询字符串中解析复杂对象)。 3)对输入数据进行字符串化。
你的ajax电话会如下所示。
$.ajax({
url: myUrl,
type: "POST",
data: JSON.stringify({
"BedID": bedID, "BedNumber": bedNumber, "ClientSearchItem": { "ClientID": 160 }, "BuildingName": buildingName,
"RoomName": roomName, "RoomNumber": roomNumber, "SiteName": siteName
}),
contentType: "application/json",
success: function (data) {
// do something here
},
error: function (error) {
alert("An error occured");
}
})
尝试一次并确认它对您有用。