简体   繁体   English

通过ajax将formdata发布到webapi

[英]post formdata via ajax to webapi

I'm looking to post formdata to ac# web api and have the model binding work. 我正在寻找将表单数据发布到ac#Web api并进行模型绑定的工作。 This looks like it should be possible, but my object is never populated. 看起来应该有可能,但是我的对象从未填充过。 I want to keep the code simple and flexible. 我想保持代码简单灵活。 ie. 即。 i dont want to add a new json attribute to my ajax call every time i add a new form field and i would like to pass files if i wish to (not expecting files to bind to the model). 我不想每次我添加一个新的表单字段时都向我的ajax调用中添加一个新的json属性,并且如果我希望(不希望文件绑定到该模型),我希望传递文件。

Here is basically what I have at the moment. 这基本上是我目前所拥有的。

$('.save').click(function () {
    $.ajax({
        url: 'api/plant/insert',
        data: new FormData($('form')[0]),
        type: 'POST',
        processData: false,
        contentType: 'application/x-www-form-urlencoded',
        beforeSend: function () {
            $.processing({ content: 'Saving Plant' });
        },
        error: function () {
            $.error({ content: 'An error occurred saving the plant' });
        },
        success: function (data) {
            location.assign('PlantEdit.cshtml?plant_id=' + data);
        }
    });
});

and in my controller 在我的控制器中

public int Insert([FromBody] Plant plant)
{
    return plant.Insert();
}

and my model 和我的模特

public class Plant : Data
{
    public int plant_id { get; set; }
    public bool live { get; set; }
    public string genus { get; set; }
    public string species { get; set; }
    public string variety_name { get; set; }

    public Plant() { }
}

and sample post data 和样本发布数据

------WebKitFormBoundaryc6x6E9JewE0ln4ql
Content-Disposition: form-data; name="live"

on
------WebKitFormBoundaryc6x6E9JewE0ln4ql
Content-Disposition: form-data; name="genus"

test genus name
------WebKitFormBoundaryc6x6E9JewE0ln4ql
Content-Disposition: form-data; name="species"

test species name
------WebKitFormBoundaryc6x6E9JewE0ln4ql
Content-Disposition: form-data; name="variety_name"

test variety name
------WebKitFormBoundaryc6x6E9JewE0ln4ql--

The result is the plant instance is not null, but all attributes are null, hence an empty row inserted into the database. 结果是工厂实例不为null,但所有属性均为null,因此将空行插入到数据库中。 So am I asking for too much, or am I going about this all wrong? 所以我要求太多了吗,还是我要把这一切都弄错了?

I would use fiddler to test the post to the service URL, but the data you are posting should be in json format I'm pretty sure: 我将使用fiddler来测试到服务URL的发布,但是您确定发布的数据应为json格式:

{ plant_id: 1, live: true, genius: "test genius", ... }

What is the output of new FormData($('form')[0]) that it's sending as the POST data? 它作为POST数据发送的new FormData($('form')[0])是什么?

Based on this answer , I believe you would need to do something like this: 基于此答案 ,我相信您将需要执行以下操作:

$('.save').click(function () {
    var fd = new FormData();    
    fd.append( 'name', $('form')[0] );

    $.ajax({
        url: 'api/plant/insert',
        data: fd,
        type: 'POST',
        processData: false,
        contentType: false,
        beforeSend: function () {
            $.processing({ content: 'Saving Plant' });
        },
        error: function () {
            $.error({ content: 'An error occurred saving the plant' });
        },
        success: function (data) {
            location.assign('PlantEdit.cshtml?plant_id=' + data);
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM