模型未在 POST 控制器方法 .Net Core MVC 中绑定
Model doesn't bind in POST controller method .Net Core MVC
我有一个模型:
public class CompanyFooterUpdateModel
{
public CompanyFooterUpdateModel(){}
public Guid CompanyId { get; set; }
public string FooterHtml { get; set; }
}
控制器:
public async Task<IActionResult> SaveFooter([FromBody] CompanyFooterUpdateModel model)
{
//.... do something in Company controller
}
视图,使用 axios post:
function SaveCompanyFooter() {
axios.post('@Url.Action("SaveFooter", "Company")',
{
model: {
FooterHtml: window.HtmlEditor.getData(),
CompanyId: "@Model.CompanyId"
}
}
).then(response => {
// .... do something
});
}
然后,我什么都试了,[FromBody],什么都试了,什么都没有...
请解释一下,我做错了什么?
UPD1:
所以我也尝试过这样的事情:
axios.post('@Url.Action("SaveFooter", "Company")',
{
"FooterHtml": window.HtmlEditor.getData(),
"CompanyId": "@Model.CompanyId"
}
)
和
public async Task<IActionResult> SaveFooter(Guid CompanyId, string FooterHtml)...
UPD2:
Controller action screenshot
JS in View
尝试从 javascript
中删除 "
axios.post('@Url.Action("SaveFooter", "Company")',
{
FooterHtml: window.HtmlEditor.getData(),
CompanyId: "@Model.CompanyId"
}
)
或尝试添加 json header
axios.post('@Url.Action("SaveFooter", "Company")',
{
FooterHtml: window.HtmlEditor.getData(),
CompanyId: "@Model.CompanyId"
}
),
{
'Content-Type': 'application/json'
})
.then((response) => {
.....
并使用此操作
public async Task<IActionResult> SaveFooter([FromBody] CompanyFooterUpdateModel model)
{
//.... do something in Company controller
}
我有一个模型:
public class CompanyFooterUpdateModel
{
public CompanyFooterUpdateModel(){}
public Guid CompanyId { get; set; }
public string FooterHtml { get; set; }
}
控制器:
public async Task<IActionResult> SaveFooter([FromBody] CompanyFooterUpdateModel model)
{
//.... do something in Company controller
}
视图,使用 axios post:
function SaveCompanyFooter() {
axios.post('@Url.Action("SaveFooter", "Company")',
{
model: {
FooterHtml: window.HtmlEditor.getData(),
CompanyId: "@Model.CompanyId"
}
}
).then(response => {
// .... do something
});
}
然后,我什么都试了,[FromBody],什么都试了,什么都没有... 请解释一下,我做错了什么?
UPD1: 所以我也尝试过这样的事情:
axios.post('@Url.Action("SaveFooter", "Company")',
{
"FooterHtml": window.HtmlEditor.getData(),
"CompanyId": "@Model.CompanyId"
}
)
和
public async Task<IActionResult> SaveFooter(Guid CompanyId, string FooterHtml)...
UPD2: Controller action screenshot
JS in View
尝试从 javascript
中删除 "axios.post('@Url.Action("SaveFooter", "Company")',
{
FooterHtml: window.HtmlEditor.getData(),
CompanyId: "@Model.CompanyId"
}
)
或尝试添加 json header
axios.post('@Url.Action("SaveFooter", "Company")',
{
FooterHtml: window.HtmlEditor.getData(),
CompanyId: "@Model.CompanyId"
}
),
{
'Content-Type': 'application/json'
})
.then((response) => {
.....
并使用此操作
public async Task<IActionResult> SaveFooter([FromBody] CompanyFooterUpdateModel model)
{
//.... do something in Company controller
}