简体   繁体   English

来自 Ajax 调用的 Asp.Net Core 中的模型绑定

[英]Model Binding in Asp.Net Core from an Ajax call

I have a Javascript function that makes a call to a Asp.Net Core controller:我有一个调用 Asp.Net Core 控制器的 Javascript 函数:

    const details =
    {
        thisModel: {
            Id: '@Model.OrderId',
            Name: '@Model.Name',
            Data: data,
            StringData: ''
        }
    };

    const data = JSON.stringify(details);

    $.ajax({                    
        type: "POST",
        url: "/Orders/CreateOrder",
        data: data,
        datatype: "json",
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            // Success!
        }
    });

I'm trying to call an Asp.Net Core controller method:我正在尝试调用一个 Asp.Net Core 控制器方法:

    [HttpPost]
    public async Task<IActionResult> CreateOrder([FromBody]OrderDetails orderDetails)
    {

The OrderDetails model looks like this: OrderDetails 模型如下所示:

public class OrderDetails
{
    public string Id { get; set; }
    public string Name { get; set; }
    public IFormFile Data { get; set; }
    public string StringData { get; set; }
}

The problem that I have is that, whilst the controller method does get called, every value inside orderDetails is null.我遇到的问题是,虽然确实调用了控制器方法,但 orderDetails 中的每个值都为空。 I've looked at the MS documentation and, as far as I can tell, I'm following all the rules, so I'm a little unsure why the data isn't being passed through.我已经查看了 MS 文档,据我所知,我遵守了所有规则,所以我有点不确定为什么没有传递数据。 Can anyone help?任何人都可以帮忙吗?

As you have an IFormFile property in your model I understand that you want to upload a file to your API.由于您的模型中有一个IFormFile属性,我知道您想将文件上传到您的 API。

The problem here is that you can't upload a file inside a JSON.这里的问题是你不能在 JSON 中上传文件。 It is a " limitation " of HTTP protocol.它是 HTTP 协议的一个“限制”。 To upload a file you should use FormData要上传文件,您应该使用FormData

Given that you already have an html form like this:鉴于您已经有一个这样的 html 表单:

<form method="post" name="fileinfo">
  <label>Your file:</label>
  <input type="file" name="data" required />
  <input type="submit" value="Upload" />
</form>

Then you should add new hidden fields to store the @Model.OrderId and @Model.Name :然后你应该添加新的隐藏字段来存储@Model.OrderId@Model.Name

<form method="post" id="fileUploadForm">
  <input type="hidden" name="OrderId" value="@Model.OrderId" />
  <input type="hidden" name="Name" value="@Model.Name" />
  <label>Your file:</label>
  <input type="file" name="Data" required />
  <input type="submit" value="Upload" />
</form>

Now on your javascript code you need to convert your forms inputs into an new FormData variable and then submit it to your api.现在在您的 javascript 代码中,您需要将表单输入转换为新的FormData变量,然后将其提交给您的 api。 The easiest way to do this is:最简单的方法是:

var form = $('#fileUploadForm')[0];

var data = new FormData(form);

$.ajax({                    
    type: "POST",
    url: "/Orders/CreateOrder",
    data: data,
    enctype: 'multipart/form-data', // Important!!!!
    processData: false,  //prevent jQuery transforming the data into a query string
    contentType: false,
    cache: false,
    success: function (data) {
        // Success!
    }
});

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

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