我有以下C#模型,但他们没有得到JSON中填充的Id或AssigneeId,请帮助吗?
public class AssignModel
{
[Required]
public Guid Id { get; set; }
public bool Clear { get; set; }
public string AssigneeId { get; set; }
}
public class MassAssignModel
{
[Required]
public List<AssignModel> Tickets { get; set; }
}
public ActionResult MassAssign(MassAssignModel model)
{
//DO stuff here
}
我尝试通过反序列化JSON来填充这些,但它永远不会得到值。这是我的javascript,第一个方法调用是massAssign():
function massAssign() {
assignTicketsTo(getSelectedTicketsIds());
}
function getSelectedTicketsIds() {
var tickets;
var selected = $('.selected-ticket:checked');
tickets = selected.map(function () {
return { Id: this.value, AssigneeId: "COMPONENTS\ASTANNARD", Clear:false };
}).get();
return tickets;
}
function assignTicketsTo(selectedTicketsIds) {
var postdata = JSON.stringify({
Tickets: [selectedTicketsIds]
});
$.ajax({
url: Helper.Util.action({ controller: "Ticket", action: "MassAssign" }),
dataType: 'json',
data: postdata,
type: 'post',
success: function (data, textStatus, jqXHR) {
},
error: function (jqXHR, textStatus, errorThrown) {
$("#load_tickets").hide();
Helper.alert(errorThrown);
}
});
}
答案 0 :(得分:1)
如果要将JSON发送到MVC控制器,则需要在contentType
中将'application/json'
设置为$.ajax
:
$.ajax({
url: Helper.Util.action({ controller: "Ticket", action: "MassAssign" }),
dataType: 'json',
contentType: 'application/json',
data: postdata,
type: 'post',
success: function (data, textStatus, jqXHR) {
},
error: function (jqXHR, textStatus, errorThrown) {
$("#load_tickets").hide();
Helper.alert(errorThrown);
}
dataType适用于
您期望从服务器返回的数据类型。
您的代码还有一个问题,当您创建postdata
时,selectedTicketsIds
变量已包含数组,因此您无需再次使用[selectedTicketsIds]
将其包装起来。
因此,请将postdata
更改为:
var postdata = JSON.stringify({
Tickets: selectedTicketsIds
});