[英]ASP.NET MVC core POST Request not converting JSON data to Model
I have a model that I serialize to the screen and use with Vue.js. 我有一个模型,我序列化到屏幕并与Vue.js一起使用。 After screen/model changes I am using axios to perform a POST request back into my ASP.NET Core (2.0) Controller.
屏幕/模型更改后我使用axios执行POST请求回到我的ASP.NET Core(2.0)控制器。 So far every combination of tries results in all null values when evaluating my custom model in my Controller.
到目前为止,在我的Controller中评估自定义模型时,每次尝试组合都会导致所有空值。 Any suggestions would be appreciated.
任何建议,将不胜感激。
Model: 模型:
[Serializable]
public class ClientsWebsite
{
[Key]
public string ClientID { get; set; }
public string CustomSiteScript { get; set; }
public string WebsiteName { get; set; }
public string FormName { get; set; }
public string SliderImagePath { get; set; }
public string MessageHeader { get; set; }
public string CallToActionMessage { get; set; }
}
Vue and POST Call: Vue和POST电话:
var sliderApp = new Vue({
el: '#sliderApp',
data: {
ClientsWebsite: [],
axajLoaded: false,
picked: []
},
created: function () {
getUserSettings()
}
})
function saveCustomSettings() {
axios.post('/Slider/Slider/SaveCustomSettings', {
vm: sliderApp.ClientsWebsite
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
Controller: 控制器:
[HttpPost]
public string SaveCustomSettings(ClientsWebsite vm)
{
if (ModelState.IsValid)
{
//stuff...
}
return "Success";
}
I have also tried using JSON.stringify in my JavaScript POST call, no changes. 我也尝试在我的JavaScript POST调用中使用JSON.stringify,没有任何更改。
Results of the POST Header Request Payload: POST标头请求有效负载的结果:
{"ClientsWebsite":{"ClientID":"6056e4a8-32a8-4042-b750-50c609edf140","CustomSiteScript":"someScriptHere","WebsiteName":"Blah.com","FormName":"Contact1","SliderImagePath":"ContactUs3","MessageHeader":"Hello, please send us a message","CallToActionMessage":"We will respond ASAP!!!!!"}}
UPDATE: The solution came from a combination of both the answers below. 更新:解决方案来自以下两个答案的组合。
In addition to what Nikolaus said, try adding [FromBody] before ClientsWebsite vm. 除了Nikolaus所说的,尝试在ClientsWebsite vm之前添加[FromBody]。
[HttpPost]
public string SaveCustomSettings([FromBody]ClientsWebsite vm)
{
if (ModelState.IsValid)
{
//stuff...
}
return "Success";
}
You have a object wrapped around your model: 你的模型周围有一个对象:
{"ClientsWebsite":{"ClientID":"6056e4a8-32a8-4042-b750-50c609edf140","CustomSiteScript":"someScriptHere","WebsiteName":"Blah.com","FormName":"Contact1","SliderImagePath":"ContactUs3","MessageHeader":"Hello, please send us a message","CallToActionMessage":"We will respond ASAP!!!!!"}}
Remove it: 去掉它:
{"ClientID":"6056e4a8-32a8-4042-b750-50c609edf140","CustomSiteScript":"someScriptHere","WebsiteName":"Blah.com","FormName":"Contact1","SliderImagePath":"ContactUs3","MessageHeader":"Hello, please send us a message","CallToActionMessage":"We will respond ASAP!!!!!"}
For your function: 为了你的功能:
function saveCustomSettings() {
axios.post('/Slider/Slider/SaveCustomSettings',
sliderApp.ClientsWebsite
)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.