简体   繁体   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

i am posting data with angular service to Webapi, the action is hitting and i am receiving data all data except DepartmentName json array.我正在使用 angular 服务向 Webapi 发布数据,该操作正在执行,并且我正在接收除DepartmentName json 数组之外的所有数据。 From frontend i am passing json array for DepartmentName and other data as you can see in attached image but i am not getting DepartmentName json array in action.从前端,我正在为DepartmentName和其他数据传递 json 数组,如您在附图中看到的,但我没有得到DepartmentName json 数组在起作用。 i am also passing one file that,s also i am getting in action but problem only for DepartmentName json array.我还传递了一个文件,我也在行动,但问题仅针对DepartmentName json 数组。 i am trying to solve this issue from last 2 days but i cant solve it.我试图从过去 2 天开始解决这个问题,但我无法解决。 i hope that you understand my question.For more explanation i have attached an image of form data and code also.我希望您能理解我的问题。有关更多解释,我还附上了表单数据和代码的图像。

Model 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 Controller

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

Angular 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>

Form-Data From Network来自网络的表单数据

在此处输入图像描述

the action is hitting and i am receiving data all data except DepartmentName json array动作正在发生,我正在接收除 DepartmentName json 数组之外的所有数据

Based on your model class, to bind form values correctly to the model, you can try following approaches:根据您的 model class,要将表单值正确绑定到 model,您可以尝试以下方法:

Approach 1: on angular client side, generate form data for List<Department> DepartmentName property, like below.方法 1:在 angular 客户端,为List<Department> DepartmentName属性生成表单数据,如下所示。

在此处输入图像描述

Approach 2: implement a custom model binder, to get DepartmentName json array that you passed and deserialize it to List<Department> .方法 2:实现自定义 model 绑定器,以获取您传递的 DepartmentName json 数组并将其反序列化为List<Department>

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

DepartmentNameModelBinder class部门名称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;
    }
}

Test Result测试结果

在此处输入图像描述

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

相关问题 是否可以将文件列表和FormData对象中的整个json对象发送到ASP.NET CORE MVC控制器 - Is it possible to send a list of files and a whole json object in a FormData object to a ASP.NET CORE MVC Controller ASP.Net Core 3 将 JSON 数组发布到控制器 - ASP.Net Core 3 Posting JSON array to controller 从ASP.net core 2.1中的url获取参数? - Get parameter from url in ASP.net core 2.1? 将JSON数组集合发布到Asp.net Core Web API - Posting JSON Array Collection to Asp.net Core Web API 从 ajax 捕获对象 - asp.net core 2.1 - Catch object from ajax - asp.net core 2.1 How to POST javascript object, javascript object list and file list to asp.net core controller - How to POST javascript object, javascript object list and file list to asp.net core controller ASP.NET Core - API 多参数 - ASP.NET Core - API multiple parameters ASP.NET Core 2.1 SignalR 未定义 - ASP.NET Core 2.1 SignalR is not defined 我正在尝试将 JSON 对象发布到 ASP.NET MVC 控制器但得到空的 JSON - I am trying to post a JSON object to a ASP.NET MVC Controller but getting empty JSON 在 ASP.Net Core 5 MVC 控制器中,当传递包含小数的 JSON 对象 FromBody 时,模型始终为空 - In ASP.Net Core 5 MVC Controller, when passed a JSON object FromBody that contains a decimal the model is always null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM