簡體   English   中英

Not getting multiple select list json array in object parameter in api controller Asp.net Core 2.1

[英]Not getting multiple select list json array in object parameter in api controller Asp.net Core 2.1

我正在使用 angular 服務向 Webapi 發布數據,該操作正在執行,並且我正在接收除DepartmentName json 數組之外的所有數據。 從前端,我正在為DepartmentName和其他數據傳遞 json 數組,如您在附圖中看到的,但我沒有得到DepartmentName json 數組在起作用。 我還傳遞了一個文件,我也在行動,但問題僅針對DepartmentName json 數組。 我試圖從過去 2 天開始解決這個問題,但我無法解決。 我希望您能理解我的問題。有關更多解釋,我還附上了表單數據和代碼的圖像。

Model

public class UserViewModel
{
    public string fullName { get; set; }
    [Column(TypeName = "nvarchar(100)")]
    public string email { get; set; }
    [Column(TypeName = "nvarchar(20)")]
    public string phone { get; set; }
    [Column(TypeName = "nvarchar(15)")]
    public string gender { get; set; }
    [Column(TypeName = "nvarchar(20)")]
    public string dateOfBirth { get; set; }
    [Column(TypeName = "nvarchar(200)")]
    public string address { get; set; }
    [Column(TypeName = "nvarchar(max)")]
    public string password { get; set; }
    [Column(TypeName = "nvarchar(max)")]
    public string userImageName { get; set; }
    public List<Department> DepartmentName { get; set; }

}



public class Department
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int DepartmentID { get; set; }
    [Column(TypeName = "nvarchar(100)")]
    public string DepartmentName { get; set; }
    public virtual ICollection<UserDepartment> UserDepartments { get; set; }
}

Controller

    [HttpPost, Route("[action]")]
    public async Task<IActionResult> AddNewUser([FromForm] UserViewModel userViewModel )
    {
       //other coding here  
       return Ok()
    }

Angular

<mat-form-field class="example-full-width">
    <mat-select placeholder="Select department(s)" formControlName="DepartmentName" multiple>
        <mat-option *ngFor="let department of departmentList" [value]="department">{{department.departmentName}}</mat-option>
    </mat-select>
</mat-form-field>

來自網絡的表單數據

在此處輸入圖像描述

動作正在發生,我正在接收除 DepartmentName json 數組之外的所有數據

根據您的 model class,要將表單值正確綁定到 model,您可以嘗試以下方法:

方法 1:在 angular 客戶端,為List<Department> DepartmentName屬性生成表單數據,如下所示。

在此處輸入圖像描述

方法 2:實現自定義 model 綁定器,以獲取您傳遞的 DepartmentName json 數組並將其反序列化為List<Department>

[ModelBinder(BinderType = typeof(DepartmentNameModelBinder))]
public List<Department> DepartmentName { get; set; }

部門名稱ModelBinder class

public class DepartmentNameModelBinder : IModelBinder
{
    public Task BindModelAsync(ModelBindingContext bindingContext)
    {
        if (bindingContext == null)
        {
            throw new ArgumentNullException(nameof(bindingContext));
        }

        // ...
        // implement it based on your actual requirement
        // code logic here
        // ...

        var options = new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true
        };

        var model = JsonSerializer.Deserialize<List<Department>>(bindingContext.ValueProvider.GetValue("DepartmentName").FirstOrDefault(), options);

        bindingContext.Result = ModelBindingResult.Success(model);
        return Task.CompletedTask;
    }
}

測試結果

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM