无法将一系列对象附加到formdata -vuejs -asp.net core

发布于 2025-02-11 09:31:09 字数 1745 浏览 1 评论 0原文

我正在尝试将一系列对象附加到FormData,但由于某种原因,我无法使其工作。看来,CreateStockingRedientdto未接收我通过的值。我一定缺少一些东西。我在这里寻找帮助。

控制器代码:

[HttpPost]
[Route("create")]
public int Create(IFormFile invoiceAttachment, [FromForm]CreateStockCommand command)
{
    // do something here.
}

请求处理程序

public class CreateStockCommand : IRequest<int>
{
    public DateTime CreatedDate { get; set; }
    public string? SupplierInvoiceId { get; set; }
    public int SupplierId { get; set; }
    public ICollection<CreateStockIngredientDto>? CreateStockIngredientDtos { get; set; 
}

public class CreateStockIngredientDto
{
    public decimal Price { get; set; }
    public decimal Quantity { get; set; }
    public DateTime StockedDate { get; set; }
    public DateTime ExpiryDate { get; set; }
    public int IngredientId { get; set; }
 }

客户端-Vuejs

const formData = new FormData()

    formData.append('invoiceAttachment', invoiceAttachment.value)
    formData.append('CreatedDate', createdDate.value)
    formData.append('SupplierInvoiceId', supplierInvoiceId.value)
    formData.append('SupplierId', supplierId.value)

    formData.append('CreateStockIngredientDtos', createStockIngredientDtos.value)

    axios.post('stock/create', formData, { headers: { 'Content-Type': 'multipart/form-data' } })
      .then(response => {
        // do something...
      })


  const createStockIngredientDtos = ref([{
  `enter code here`ingredientId: 1, price: 200, quantity: 20, expiryDate: '2022-06-29',
}])

”在此处输入图像描述”

I'm trying to append an array of objects to FormData but I couldn't get this to work for some reason. As it appears, CreateStockIngredientDto doesn't receive values that I pass. I must be missing something. I'm looking for a helping hand here.

Controller Code:

[HttpPost]
[Route("create")]
public int Create(IFormFile invoiceAttachment, [FromForm]CreateStockCommand command)
{
    // do something here.
}

Request Handler

public class CreateStockCommand : IRequest<int>
{
    public DateTime CreatedDate { get; set; }
    public string? SupplierInvoiceId { get; set; }
    public int SupplierId { get; set; }
    public ICollection<CreateStockIngredientDto>? CreateStockIngredientDtos { get; set; 
}

public class CreateStockIngredientDto
{
    public decimal Price { get; set; }
    public decimal Quantity { get; set; }
    public DateTime StockedDate { get; set; }
    public DateTime ExpiryDate { get; set; }
    public int IngredientId { get; set; }
 }

Client Side - Vuejs

const formData = new FormData()

    formData.append('invoiceAttachment', invoiceAttachment.value)
    formData.append('CreatedDate', createdDate.value)
    formData.append('SupplierInvoiceId', supplierInvoiceId.value)
    formData.append('SupplierId', supplierId.value)

    formData.append('CreateStockIngredientDtos', createStockIngredientDtos.value)

    axios.post('stock/create', formData, { headers: { 'Content-Type': 'multipart/form-data' } })
      .then(response => {
        // do something...
      })


  const createStockIngredientDtos = ref([{
  `enter code here`ingredientId: 1, price: 200, quantity: 20, expiryDate: '2022-06-29',
}])

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哥,最终变帅啦 2025-02-18 09:31:09

我终于想出了。我希望它能希望其他人。

    const formData = new FormData()

    formData.append('invoiceAttachment', invoiceAttachment.value)
    formData.append('CreatedDate', createdDate.value)
    formData.append('SupplierInvoiceId', supplierInvoiceId.value)
    formData.append('SupplierId', supplierId.value)

    for (let i = 0; i < createStockIngredientDtos.value.length; i++) {
      formData.append(`CreateStockIngredientDtos[${i}].ingredientId`, createStockIngredientDtos.value[i].ingredientId)
      formData.append(`CreateStockIngredientDtos[${i}].price`, createStockIngredientDtos.value[i].price)
      formData.append(`CreateStockIngredientDtos[${i}].quantity`, createStockIngredientDtos.value[i].quantity)
      formData.append(`CreateStockIngredientDtos[${i}].expiryDate`, createStockIngredientDtos.value[i].expiryDate)
    }

    axios.post('stock/create', formData, { headers: { 'Content-Type': 'multipart/form-data' } })
      .then(response => {
        if (response.data !== undefined) {
        // do something...
        }
      })

I figured it out finally. I hope it will hope someone else.

    const formData = new FormData()

    formData.append('invoiceAttachment', invoiceAttachment.value)
    formData.append('CreatedDate', createdDate.value)
    formData.append('SupplierInvoiceId', supplierInvoiceId.value)
    formData.append('SupplierId', supplierId.value)

    for (let i = 0; i < createStockIngredientDtos.value.length; i++) {
      formData.append(`CreateStockIngredientDtos[${i}].ingredientId`, createStockIngredientDtos.value[i].ingredientId)
      formData.append(`CreateStockIngredientDtos[${i}].price`, createStockIngredientDtos.value[i].price)
      formData.append(`CreateStockIngredientDtos[${i}].quantity`, createStockIngredientDtos.value[i].quantity)
      formData.append(`CreateStockIngredientDtos[${i}].expiryDate`, createStockIngredientDtos.value[i].expiryDate)
    }

    axios.post('stock/create', formData, { headers: { 'Content-Type': 'multipart/form-data' } })
      .then(response => {
        if (response.data !== undefined) {
        // do something...
        }
      })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文