[英]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.