英文:
Passing a nested list of objects with objects in post ajax method for web api
问题
以下是翻译好的部分:
I am stuck in situation where I have nested list of objects inside objects. I am stuck and can't move any further.
(我陷入了一个情况,其中我在对象内部有嵌套的对象列表。我卡住了,无法继续前进。)
Working fine without passing oNestedClass.
(不传递 oNestedClass 时运行正常。)
And I'm having trouble passing a list of objects via JSON to Web Api.
(而且我在通过 JSON 将对象列表传递到 Web Api 中遇到了问题。)
The controller method:
(控制器方法:)
[HttpPost("SaveDocuments")]
public IActionResult SaveDocuments(Documenttest saveDocs)
{
var docTypeList = docContext.SaveDocuments(saveDocs);
return Ok(docTypeList);
}
Class:
(类:)
public class Documenttest
{
public int Id { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? Gender { get; set; }
public List
}
public class NestedClass
{
public string? StdType { get; set; }
public IFormFile? StdFile { get; set; }
}
Calling from .aspx page:
(从 .aspx 页面调用:)
$("#postform").submit(function (e) {
var apiurl = "https://localhost:7168/api/NestClass/SaveDocuments?";
var fileUpload = $(".uplFile");
var stdDetails = [];
for (var i = 0; i < fileUpload.length; i++) {
var files = fileUpload[i];
if (files.files.length > 0) {
debugger
stdDetails.push({ "StdType ": i, "StdFile ": files.files[i] });
}
}
var data = {
"FirstName": $("#txtFirstName").val(),
"LastName": $("#txtLastName").val(),
"Gender": $("#txtGender").val(),
"oNestedClass": JSON.stringify(stdDetails)
}
$.ajax({
type: 'POST',
url: apiurl,
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (d) {
alert("Saved Successfully");
},
error: function () {
alert("Error please try again");
}
});
e.preventDefault();
});
英文:
I am stuck in situation where I have nested list of objects inside objects. I am stuck and can't move any further.
Working fine without passing oNestedClass.
And I'm having trouble passing a list of objects via JSON to Web Api.
The controller method:
[HttpPost("SaveDocuments")]
public IActionResult SaveDocuments(Documenttest saveDocs)
{
var docTypeList = docContext.SaveDocuments(saveDocs);
return Ok(docTypeList);
}
Class:
public class Documenttest
{
public int Id { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? Gender { get; set; }
public List<NestedClass>? oNestedClass { get; set; } = new();
}
public class NestedClass
{
public string? StdType { get; set; }
public IFormFile? StdFile { get; set; }
}
Calling from .aspx page:
$("#postform").submit(function (e) {
var apiurl = "https://localhost:7168/api/NestClass/SaveDocuments?";
var fileUpload = $(".uplFile");
var stdDetails = [];
for (var i = 0; i < fileUpload.length; i++) {
var files = fileUpload[i];
if (files.files.length > 0) {
debugger
stdDetails.push({ "StdType ": i, "StdFile ": files.files[i] });
}
}
var data = {
"FirstName": $("#txtFirstName").val(),
"LastName": $("#txtLastName").val(),
"Gender": $("#txtGender").val(),
"oNestedClass": JSON.stringify(stdDetails)
}
$.ajax({
type: 'POST',
url: apiurl,
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (d) {
alert("Saved Successfully");
},
error: function () {
alert("Error please try again");
}
});
e.preventDefault();});
答案1
得分: 1
尝试使用 formdata
来传递文件。注意,我们传递的数据是通过名称属性绑定的,比如:saveDocs.oNestedClass[" + i + "].StdFile
,以保持与 API 操作参数 saveDocs 一致。
我有一个工作示例如下,您可以参考它:
- 将您的代码更改为:
$("#postform").submit(function (e) {
var apiurl = "https://localhost:7019/api/NestClass/SaveDocuments";
var fileUpload = $(".uplFile");
var formData = new FormData();
for (var i = 0; i < fileUpload.length; i++) {
var files = fileUpload[i];
if (files.files.length > 0) {
debugger
formData.append("saveDocs.oNestedClass[" + i + "].StdFile", files.files[i]);
formData.append("saveDocs.oNestedClass[" + i + "].StdType", i);
}
}
formData.append("saveDocs.FirstName", $("#txtFirstName").val());
formData.append("saveDocs.LastName", $("#txtLastName").val());
formData.append("saveDocs.Gender", $("#txtGender").val());
$.ajax({
type: 'POST',
url: apiurl,
data: formData,
contentType: false,
processData: false,
success: function (d) {
alert("Saved Successfully");
},
error: function () {
alert("Error please try again");
}
});
e.preventDefault();
});
- 添加
[FromForm]
以接收formdata
。
[HttpPost("SaveDocuments")]
public IActionResult SaveDocuments([FromForm]Documenttest saveDocs)
{
var docTypeList = docContext.SaveDocuments(saveDocs);
return Ok(docTypeList);
}
结果:
英文:
Try to use formdata
to pass the file. Note we pass the data binded by name attribute, such us: saveDocs.oNestedClass[" + i + "].StdFile
, to keep same as the api action parameter saveDocs.
I have a work demo like below, you can refer to it:
1.change your code like :
$("#postform").submit(function (e) {
var apiurl = "https://localhost:7019/api/NestClass/SaveDocuments";
var fileUpload = $(".uplFile");
var formData = new FormData();
for (var i = 0; i < fileUpload.length; i++) {
var files = fileUpload[i];
if (files.files.length > 0) {
debugger
formData.append("saveDocs.oNestedClass[" + i + "].StdFile", files.files[i]);
formData.append("saveDocs.oNestedClass[" + i + "].StdType", i);
}
}
formData.append("saveDocs.FirstName", $("#txtFirstName").val());
formData.append("saveDocs.LastName", $("#txtLastName").val());
formData.append("saveDocs.Gender", $("#txtGender").val());
$.ajax({
type: 'POST',
url: apiurl,
data: formData,
contentType: false,
processData: false,
success: function (d) {
alert("Saved Successfully");
},
error: function () {
alert("Error please try again");
}
});
e.preventDefault();
});
2.add [FromForm]
to recieve formdata
[HttpPost("SaveDocuments")]
public IActionResult SaveDocuments([FromForm]Documenttest saveDocs)
{
var docTypeList = docContext.SaveDocuments(saveDocs);
return Ok(docTypeList);
}
result:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论