如何绑定包含 IEnumerables class 的模型
How to Bind a Model that contains an IEnumerables class
我有一个前端在 ASP.NET Core 中调用对 Web api 的 axios 请求,由于某种原因我的模型无法绑定。我正在将电子邮件数据发送到网络 api 并尝试将其绑定到电子邮件和联系人模型。它只是一直将电子邮件对象显示为空。我的 axios 请求似乎工作正常,因为它确实发送了有效负载,但由于电子邮件对象为空,API 中出现错误。
JS
axios({
method: 'post',
url: window.location.origin + '/MainReview/SendEmail',
data: {
From: this.emailFrom,
To: this.emailToModel,
Cc: this.emailCcModel,
Bcc: this.emailBccModel,
Subject: this.emailSubject,
Body: this.emailBody,
},
headers: {
'Content-Type': 'multipart/form-data',
"RequestVerificationToken": document.forms[0].querySelector('input[name="__RequestVerificationToken"]').value,
}
}).then(response => {
}).catch(error => {
console.log(error.response.data.error);
});
网络API
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SendEmail([FromBody] Email email)
{
System.Diagnostics.Debug.WriteLine(email.From);
return Ok(new { Result = email.From });
}
型号:
public class Email
{
public string? From { get; set; }
public IEnumerable<Contact>? To { get; set; }
public IEnumerable<Contact>? Cc { get; set; }
public IEnumerable<Contact>? Bcc { get; set; }
public string? Subject { get; set; }
public string? Body { get; set; }
}
public class Contact
{
public string EmailAddress { get; set; }
}
Json 负载中发送的数据:
{ "From":"emailfrom@example.com", "To":[ { "EMAIL_ADDRESS":"emailTo@example.com" }, { "EMAIL_ADDRESS":"emailCC@example.com" } ], "Cc":[ { "EMAIL_ADDRESS":"emailCC2@example.com" } ], "Bcc":[ ], "Subject":"Temporary Placeholder", "Body":"tessdf" }
我认为我的问题与 IEnumerable 有关。老实说,我一直远离模型,但我想我会开始尝试更多地使用它们,因为它是最佳实践。但是,这让我抓狂,因为我不明白为什么这行不通。
确保结构匹配方法参数class。
在这里,我们有一个电子邮件参数,它是一个对象 {}。它还有一些属性,这些属性是对象列表 [{}].
data: {
From: this.emailFrom,
To: [{ EmailAddress: this.emailToModel }],
Cc: [{ EmailAddress: this.emailCcModel }],
Bcc: [{ EmailAddress: this.emailBccModel }],
Subject: this.emailSubject,
Body: this.emailBody,
}
注意:如果您传递的参数超过一个,您还需要指定参数名称,以及一组额外的 {}。
data: {
email: {
From: this.emailFrom,
To: [{ EmailAddress: this.emailToModel }],
Cc: [{ EmailAddress: this.emailCcModel }],
Bcc: [{ EmailAddress: this.emailBccModel }],
Subject: this.emailSubject,
Body: this.emailBody,
},
parameter2:"XXXXXXXXX"
}
1.You 后端使用 [FromBody]
前端使用 post json 数据,Content-Type
应该是 application/json
.
headers: {
'Content-Type': 'application/json', //change here....
"RequestVerificationToken": document.forms[0].querySelector('input[name="__RequestVerificationToken"]').value,
}
2.The posted json 数据不正确,因为 Contact
模型中的 属性 名为 EmailAddress
,但您 post json 名称为 EMAIL_ADDRESS
.
data: { "From":"emailfrom@example.com", "To":[ { "EmailAddress":"emailTo@example.com" }, { "EmailAddress":"emailCC@example.com" } ], "Cc":[ { "EmailAddress":"emailCC2@example.com" } ], "Bcc":[ ], "Subject":"Temporary Placeholder", "Body":"tessdf" },
我有一个前端在 ASP.NET Core 中调用对 Web api 的 axios 请求,由于某种原因我的模型无法绑定。我正在将电子邮件数据发送到网络 api 并尝试将其绑定到电子邮件和联系人模型。它只是一直将电子邮件对象显示为空。我的 axios 请求似乎工作正常,因为它确实发送了有效负载,但由于电子邮件对象为空,API 中出现错误。
JS
axios({
method: 'post',
url: window.location.origin + '/MainReview/SendEmail',
data: {
From: this.emailFrom,
To: this.emailToModel,
Cc: this.emailCcModel,
Bcc: this.emailBccModel,
Subject: this.emailSubject,
Body: this.emailBody,
},
headers: {
'Content-Type': 'multipart/form-data',
"RequestVerificationToken": document.forms[0].querySelector('input[name="__RequestVerificationToken"]').value,
}
}).then(response => {
}).catch(error => {
console.log(error.response.data.error);
});
网络API
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SendEmail([FromBody] Email email)
{
System.Diagnostics.Debug.WriteLine(email.From);
return Ok(new { Result = email.From });
}
型号:
public class Email
{
public string? From { get; set; }
public IEnumerable<Contact>? To { get; set; }
public IEnumerable<Contact>? Cc { get; set; }
public IEnumerable<Contact>? Bcc { get; set; }
public string? Subject { get; set; }
public string? Body { get; set; }
}
public class Contact
{
public string EmailAddress { get; set; }
}
Json 负载中发送的数据:
{ "From":"emailfrom@example.com", "To":[ { "EMAIL_ADDRESS":"emailTo@example.com" }, { "EMAIL_ADDRESS":"emailCC@example.com" } ], "Cc":[ { "EMAIL_ADDRESS":"emailCC2@example.com" } ], "Bcc":[ ], "Subject":"Temporary Placeholder", "Body":"tessdf" }
我认为我的问题与 IEnumerable 有关。老实说,我一直远离模型,但我想我会开始尝试更多地使用它们,因为它是最佳实践。但是,这让我抓狂,因为我不明白为什么这行不通。
确保结构匹配方法参数class。 在这里,我们有一个电子邮件参数,它是一个对象 {}。它还有一些属性,这些属性是对象列表 [{}].
data: {
From: this.emailFrom,
To: [{ EmailAddress: this.emailToModel }],
Cc: [{ EmailAddress: this.emailCcModel }],
Bcc: [{ EmailAddress: this.emailBccModel }],
Subject: this.emailSubject,
Body: this.emailBody,
}
注意:如果您传递的参数超过一个,您还需要指定参数名称,以及一组额外的 {}。
data: {
email: {
From: this.emailFrom,
To: [{ EmailAddress: this.emailToModel }],
Cc: [{ EmailAddress: this.emailCcModel }],
Bcc: [{ EmailAddress: this.emailBccModel }],
Subject: this.emailSubject,
Body: this.emailBody,
},
parameter2:"XXXXXXXXX"
}
1.You 后端使用 [FromBody]
前端使用 post json 数据,Content-Type
应该是 application/json
.
headers: {
'Content-Type': 'application/json', //change here....
"RequestVerificationToken": document.forms[0].querySelector('input[name="__RequestVerificationToken"]').value,
}
2.The posted json 数据不正确,因为 Contact
模型中的 属性 名为 EmailAddress
,但您 post json 名称为 EMAIL_ADDRESS
.
data: { "From":"emailfrom@example.com", "To":[ { "EmailAddress":"emailTo@example.com" }, { "EmailAddress":"emailCC@example.com" } ], "Cc":[ { "EmailAddress":"emailCC2@example.com" } ], "Bcc":[ ], "Subject":"Temporary Placeholder", "Body":"tessdf" },