我有一个ajax请求,从输入字段获取字符串,然后将其发送到服务器,期望接收对象集合。我应该收到一个Notes集合,其中的注释包含对象Logbook,对象Category,对象User,但是,如果删除这些对象但不包括它们,则说明解析工作正常。
检索集合的方式:
card
如果我只是删除我包含的四个包含项,则解析工作正常!我应该如何使用包含的对象来检索集合?
Ajax调用
public ICollection<NoteViewModel> SearchByTextAsync(string text)
{
var notes = this.dbContext.Notes
//.Include(l => l.Logbook)
// .ThenInclude(x => x.LogbookManagers)
//.Include(x => x.Logbook)
// .ThenInclude(s => s.Business)
//.Include(c => c.Category)
//.Include(u => u.User)
.Where(n => n.Text.Contains(text))
.ToList();
var mappedNotes = this.mappingProvider.MapTo<ICollection<NoteViewModel>>(notes);
return mappedNotes;
}
控制器
$("#target").keyup(function (event) {
var request = $.ajax({
url: "/Management/Management/GetNotesAsyncJson",
type: "POST",
data: { "data": event.target.value },
dataType: 'json'
});
request.done(function (data) {
$.each(data, function (index) {
var textToPrepend =
"<div class='pricing-option' id='idPlace'>" +
"<i class='material-icons'> mode_comment</i>" +
"<h1 class='note-title' id='titlePlace'>admin@admin.admin</h1>" +
"<hr />" +
"<p id='textPlace'>" + data[index].text + "</p>" +
"<hr />" +
"<p id='priorityPlace'>" + data[index].prioritytype + "</p>" +
"<hr />" +
"<div class='price'>" +
"<div class='front'>" +
"<span class='note-title'>@Model.Category?.Name </span>" +
"</div>" +
"<div class='back'>" +
"<i class='material-icons' id='editNote' data-Id='@Model.Id'>edit</i>" +
"<i class='material-icons' id='deleteNote' data-Id='@Model.Id'>remove_circle</i>" +
"<i class='material-icons' id='archiveNote'>archive</i>" +
"</div>" +
"</div>" +
"</div>";
$('.pricing-table').prepend(textToPrepend)
});
})
request.fail(function (data) {
console.log(data);
console.log(data.responseText)
})
});
答案 0 :(得分:1)
在您的控制器中
[HttpPost({data})]
public JsonResult GetNotesAsyncJson(string data)
{
var notes = this.noteService.SearchByTextAsync(data);
var model = new SearchViewModel();
model.Notes = notes;
return Json(model.Notes);
}
如果这样不起作用,请尝试在您的Ajax代码中使用JSON.stringify之类的东西
$("#performActionButton").click(function (event) {
var data = { data: event.target.value };
$.ajax({
url: '/url',
data: data,
type: 'POST',
traditional: true,
contentType: 'application/json; charset=utf-8',
success: function (data) {
}
});
});
答案 1 :(得分:1)
解决方案:Json处于自引用循环中,无法解析属性。我通过在导致此问题的属性中添加[JsonIgnore]来解决了这个问题。