简体   繁体   English

ASP.NET MVC核心POST请求不将JSON数据转换为Model

[英]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.

相关问题 在 Z9E0DA8438E1E38A1C30F4B76CE7B3 中将 Model 转换为 JSON object - Converting a Model to a JSON object in ASP.NET Core MVC ASP.NET MVC核心Web POST请求使用NULL值填充数据模型 - ASP.NET MVC Core Web POST Request populating Data Model with NULL values 在ASP.Net Core MVC中读取JSON post数据 - Read JSON post data in ASP.Net Core MVC Asp.net core 3.1 mvc post request model 绑定简单类型 - Asp.net core 3.1 mvc post request model binding for simple types ASP.NET 核心 MVC - 为什么 Ajax JSON 请求在 POST 上返回 400 而不是 403 - ASP.NET Core MVC - Why does Ajax JSON request return 400 on POST instead of 403 控制器中带有 FormCollection 模型的 ASP.NET CORE MVC 发布表单 - ASP.NET CORE MVC post form with FormCollection Model in controller 将复杂模型的表单发布到ASP.NET Core MVC中的控制器 - Form Post of an Complex Model to Controller in ASP.NET Core MVC 模型绑定不适用于 ASP.NET Core 2 WebAPI 中的 POST 请求 - Model binding is not working on POST request in ASP.NET Core 2 WebAPI ASP.NET Core MVC - 数据没有从视图传递到 controller 在 post 请求中 - ASP.NET Core MVC - Data does not pass from the view to the controller in a post request 在post request asp.net mvc中更改模型属性 - Change model property in post request asp.net mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM