我试图将包含javascript中的字符串数组的对象发布到API c#asp.net但是API由于某种原因将该数组作为单个字符串接收而不是作为字符串数组。 请注意,问题发生的唯一部分是电子邮件属性 Javascript代码:
vm =
{
title: $("#formTitle").val(),
email: function () {
var arr = [];
document.getElementById("issueUserList").childNodes.forEach(function(child) {
arr.push(child.innerText);
});
return arr;
},
urgencyFlag: parseInt($("#formUrgencyFlag").val()),
completionFlag: parseInt($("#formCompletionFlag").val()),
category: {
id: $("#category").val(),
description: $("#category :selected").attr("name")
},
issueTagsId: $("#tags").val().map(Number),
description: $("#formDescription").val(),
solution: $("#formSolution").val(),
note: $("#formNote").val()
}
$.ajax({
url: "/api/issues",
method: "POST",
data: vm
})
.done(function() {
clearIssueForm();
toastr.success("Issue recorded successfully");
})
.fail(function(xhr) {
if (xhr.status === 404) {
toastr.error(xhr.responseText
.replace("message", "User")
.replace("{", "")
.replace("}", ""));
} else {
console.log(xhr.responseText);
toastr.error("something unexpected has occured. Please try again");
}
});
}
});
现在,当我调试这个时,它正确地构建了一个字符串数组。
应该收到此代码的Api c#
[HttpPost]
public IHttpActionResult CreateIssue(IssueDto dto)
{
var users = _context.Persons.Where(p => dto.Email.Contains(p.Email)).ToList();
var issueTags = _context.IssueTagses.Where(
t => dto.IssueTagsId.Contains(t.Id)).ToList();
var issue = new Issue
{
Title = dto.Title,
Description = dto.Description,
Solution = dto.Solution,
Note = dto.Note,
UrgencyFlag = dto.UrgencyFlag,
CompletionFlag = dto.CompletionFlag,
DateIssueRegistered = DateTime.Now,
LastUpdate = null,
CategorieId = dto.Category.Id,
Persons = users,
IssueTagses = issueTags
};
_context.Issues.Add(issue);
_context.SaveChanges();
dto.Id = issue.Id;
return Created(new Uri(Request.RequestUri +"/" + dto.Id), dto );
}
IssueDto的结构
public class IssueDto
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Solution { get; set; }
public string Note { get; set; }
public List<string> Email { get; set; } //Extra
public UrgencyFlag UrgencyFlag { get; set; }
public CompletionFlag CompletionFlag { get; set; }
public DateTime DateIssueRegistered { get; set; }
public DateTime? LastUpdate { get; set; }
public IssueCategorieDto Category { get; set; }
public List<PersonDto> PersonDtos { get; set; }
public List<IssueTagsDto> IssueTags { get; set; }
public List<int> IssueTagsId { get; set; } // Extra info
public int CategorieId { get; set; }
然后当使用jquery $ .ajax发布到api时,api将此数组转换为单个字符串作为示例&#34; name1,name2&#34;而不是[name1],[name2]因此我的_context.Persons.Where代码无法找到任何与数据库中的匹配的东西,因为用户不存在。 任何可能发生这种情况的想法或了解问题所需的任何其他信息?
答案 0 :(得分:3)
你需要调用这个函数: -
vm = {
title: $("#formTitle").val(),
email: function() {
var arr = [];
document.getElementById("issueUserList").childNodes.forEach(function(child) {
arr.push(child.innerText);
});
return arr;
}(), // <--- HERE
urgencyFlag: parseInt($("#formUrgencyFlag").val()),
completionFlag: parseInt($("#formCompletionFlag").val()),
category: {
id: $("#category").val(),
description: $("#category :selected").attr("name")
},
issueTagsId: $("#tags").val().map(Number),
description: $("#formDescription").val(),
solution: $("#formSolution").val(),
note: $("#formNote").val()
}
这将在创建对象后立即调用该函数,将属性设置为数组。