我正在尝试在数组中添加数据。添加后,一旦用户单击“提交”,数据就会转换为JSON对象并调用asp.net核心服务。我传递到后端的数据为空
<mat-form-field>
<mat-accordion class="example-headers-align" *ngIf="concatArray">
<mat-expansion-panel *ngFor="let item of concatArray" hideToggle>
<mat-expansion-panel-header>
<mat-panel-title>
{{item[0].key}}
</mat-panel-title>
<mat-panel-description>
Need To Improve?
<mat-icon matSuffix>sentiment_very_satisfied</mat-icon>
</mat-panel-description>
</mat-expansion-panel-header>
<mat-label>
<mat-checkbox *ngFor="let dish of item[0].value" (click)="onItemClicked(dish)" class="example-margin"
[labelPosition]="labelPosition" [disabled]="disabled">
{{dish.dishName}}
</mat-checkbox>
</mat-label>
</mat-expansion-panel>
<mat-action-row>
<button mat-button color="warm" (click)="onSubmit()">Submit</button>
</mat-action-row>
</mat-accordion>
</mat-form-field>
export class FeedbackComponent implements OnInit {
panelOpenState = true;
public dishMenu: Dishes[];
public dishes: Dishes[] = [];
checked = false;
concatArray = [];
indeterminate = false;
labelPosition = 'after';
isSelected = false;
disabled = false;
commentList: string[] = ['Extra Cooked', 'Oily', 'Not Cooked', 'Taste', 'Others'];
ngOnInit(): void {
this.dishService.getDishes().subscribe(result => {
this.dishMenu = result.dishes;
this.concatArray = _.chain(this.dishMenu)
.groupBy(x => x.type)
.map((item, description) => [{ key: description, value: item }])
.value();
console.log(this.concatArray);
});
}
constructor(@Inject(MAT_BOTTOM_SHEET_DATA) public data: any,
// tslint:disable-next-line:align
private dishService: DishMenuService, public dialog: MatDialog, private commonService: CommonService) {
}
onItemClicked(dish: Dishes) {
console.log(dish);
const dishFeedback = new Dishes(dish);
dishFeedback.dishRating = this.data.rating;
dishFeedback.tenantId = this.data.userId;
this.dishes.push(dishFeedback);
console.log(this.dishes);
}
onSubmit() {
console.log(this.dishes);
this.dishService.submitDishFeedback(this.dishes).subscribe(result => {
if (result) {
const validate = new ValidationModel();
validate.validationMessage = 'Thank you!!';
validate.validationType = ValidationType.success;
this.dialog.closeAll();
this.commonService.openSnackBar(validate);
}
});
}
}
export class DishMenuService {
constructor(private urlEndPoint: YNGBUrlEndPoints, private httpClient: HttpClient
) {
}
submitDishFeedback(dish: Dishes[]) {
const dishFeedBackUrl = this.urlEndPoint.getDishFeedbackUrl();
var dishes=JSON.stringify(dish);
return this.httpClient.post(dishFeedBackUrl, {"dishModel":dishes})
.pipe(map(result => result));
}
}
public class DishModel
{
public List<DishFeedbackModel> Dishes { get; set; }
}
public class DishMain
{
public string DishId { get; set; }
public string DishName { get; set; }
public string Type { get; set; }
}
public class DishFeedbackModel : DishMain
{
public int DishRating { get; set; }
public string Comment { get; set; }
public string TenantId { get; set; }
}
[Route("api/[controller]")]
[ApiController]
public class DishFeedbackController : ControllerBase
{
private readonly IDishFeedbackRepository _dishFeedbackRepository;
public DishFeedbackController(IDishFeedbackRepository dishFeedbackRepository)
{
_dishFeedbackRepository = dishFeedbackRepository;
}
[HttpPost]
public async Task<bool> Post([FromBody]DishModel dishModel)
{
if (dishModel == null)
{
throw new NullReferenceException("Value of DishModel cannot be null");
}
var result = await _dishFeedbackRepository.AddDishFeedback(dishModel);
return result;
}
}
我从前端提交的数据为空,我需要将其与列表菜映射