尝试将多个表单字段绑定到一个模型

问题描述

也许我问的是完全愚蠢的,但是我找不到任何将多个表单字段绑定到模型的一个(复杂)属性的示例。

从我的单元测试中提取

...
            var formData = new MultipartFormDataContent(formDataBoundary)
            {
                { new StringContent("0123456789"),"did" },{ new StringContent("A"),"connectionStatus" },{ new StringContent("Access Only 2432/160"),"descriptionOffer" },{ new StringContent("ADSN4DSL"),"codeOffer" },{ new StringContent("norMAL"),"supportLevel" },{ new StringContent("Entreprise TEST"),"finalCustomerDenomination" },{ new StringContent("RUE"),"finalCustomerStreetCode" },{ new StringContent("du Test"),"finalCustomerStreetName" },{ new StringContent("45"),"finalCustomerStreetNumber" },{ new StringContent("69389"),"finalCustomerCityCode" },{ new StringContent("69009"),"finalCustomerCityPostalCode" },{ new StringContent("Lyon 9e"),"finalCustomerCityName" },{ new StringContent("TestCustomer"),"finalCustomerContactName" },{ new StringContent("0987654321"),"finalCustomerContactPhoneNumber" },{ new StringContent("TestOperator"),"operatorContactName" },{ new StringContent("0512346789"),"operatorContactPhoneNumber" },{ new StringContent("Un commentaire opérateur"),"operatorComment" },{ new StringContent("Une note..."),"note" },};

            var response = await client
                .PostAsync(CREATION_URL,formData)
                .ConfigureAwait(false);
...

从我的控制器中提取

        [HttpPost]
        public async Task<IActionResult> Post(
            [FromForm] CreationorderInput input)
{
...

从我的输入模型中提取

    public sealed class CreationorderInput
    {
        public frenchPhoneNumber Did { get; set; }
        public Address FinalCustomerAddress { get; set; }
...
    }

    public class Address
    {
        public virtual NonEmptyString? Designation { get; set; }
        public virtual NonEmptyString StreetLabel { get; set; }
        public virtual NonEmptyString? StreetCode { get; set; }
        public virtual NonEmptyString? StreetNumber { get; set; }
        public virtual NonEmptyString? Cluster { get; set; }
        public virtual NonEmptyString? Building { get; set; }
        public virtual NonEmptyString? Stair { get; set; }
        public virtual NonEmptyString? Floor { get; set; }
        public virtual NonEmptyString? Door { get; set; }
        public virtual NonEmptyString? logo { get; set; }
        public virtual NonEmptyString? CityCode { get; set; }
        public virtual NonEmptyString CityPostalCode { get; set; }
        public virtual NonEmptyString CityName { get; set; }
    }

基本上,我正在尝试创建一个地址 binder 以将finalCustomerXXX绑定到Address的多个属性,但是IModelBinder.BindModelAsync对我来说似乎很模糊。

任何帮助将不胜感激。

更新

我无法控制POST和字段名称

解决方法

您似乎想将复杂的对象传递给post方法。

由于您提供的类的字段与MultipartFormDataContent中提供的某些字段名称不一致,因此我将简化您的类以使代码更清晰

基本上,我正在尝试制作一个地址绑定器来绑定 finalCustomerXXX到Address的几个属性

如果您想以finalCustomerXXX的形式绑定相应类下的字段,则必须首先确保XXX之前的内容是字段名相应的类,需要将它们用点号(。)隔开,或将XXX放在方括号中,该方括号([])如下:

 FinalCustomerAddress.XXX or FinalCustomerAddress[XXX]

这里FinalCustomerAddressAddress类中CreationOrderInput类型的字段名称,而XXX是{CityCode之类的Address类的字段名称。>

以下是一个完整的案例,请参考:

public sealed class CreationOrderInput
{
    public string Name { get; set; } 
    public FrenchPhoneNumber Did { get; set; }
    public Address FinalCustomerAddress { get; set; }
}

public class FrenchPhoneNumber
{
    public string number { get; set; }
}
public class Address
{
    public virtual NonEmptyString? CityCode { get; set; }
    public virtual NonEmptyString CityPostalCode { get; set; }
    public virtual NonEmptyString CityName { get; set; }
}

测试:

  var formData = new MultipartFormDataContent(formDataBoundary)
        {
            { new StringContent("Lisa"),"Name" },{ new StringContent("0123456789"),"Did[number]" },{ new StringContent("69389"),"FinalCustomerAddress[CityCode]" },{ new StringContent("69009"),"FinalCustomerAddress[CityPostalCode]" },{ new StringContent("Lyon 9e"),"FinalCustomerAddress[CityName]" },//or
             //{ new StringContent("0123456789"),"Did.number" },//{ new StringContent("69389"),"FinalCustomerAddress.CityCode" },//{ new StringContent("69009"),"FinalCustomerAddress.CityPostalCode" },//{ new StringContent("Lyon 9e"),"FinalCustomerAddress.CityName" },};
             var response = await client
            .PostAsync(CREATION_URL,formData)
            .ConfigureAwait(false);
       



  

这是测试结果:

enter image description here