how i can solve this problem (serialized/ and deserialize JSON array and store and read from sql server)? thank you
i created asp.net core api that connected to angular project and occurs problem when i want post json that contains json array(ingredient) :
{ "RecepieName": "3222",
"Ingredient": [ { "ingredientName": "43243", "value": "33", "measure": "" },
{ "ingredientName": "565" , "value": "3", "measure": "" }],
"CookingTime": 3,
"Level": "advance",
"ServingPerson": 3,
"RecepieAbstration": "good food",
"RecepieText": "",
"CreateDate": "2021-01-04T12:37:51.948",
"ModifiedDate": "2021-01-04T12:37:51.948" }
you can see my model , dto and action post in controller and DbContext :
Model :
- public class Recepie
- {
- [Key]
- public int ID { get; set; }
- [Required]
- public string RecepieName { get; set; }
- [Required]
- public string Ingredient { get; set; }
- [Required]
- public int CookingTime { get; set; }
- [Required]
- public string Level { get; set; }
- [Required]
- public int ServingPerson { get; set; }
- [Required]
- public string RecepieAbstration { get; set; }
- [Required]
- public string RecepieText { get; set; }
- [Required]
- public string NutritionalInformation { get; set; }
- [Required]
- public string Tags { get; set; }
- public DateTime CreateDate { get; set; }
- public DateTime ModifiedDate { get; set; }
- }
DTO : RecepieCreateDto
- public class RecepieCreateDto
- public string RecepieName { get; set; }
-
- public string Ingredient { get; set; }
- public int CookingTime { get; set; }
- public string Level { get; set; }
- public int ServingPerson { get; set; }
- public string RecepieAbstration { get; set; }
- public string RecepieText { get; set; }
- public string Tags { get; set; }
- public DateTime CreateDate { get; set; }
- public DateTime ModifiedDate { get; set; }
Action Post :
-
- [HttpPost]
- public ActionResult <RecepieReadDto> CreateRecepie(RecepieCreateDto recepieCreateDto)
- {
- var recepieModel = _mapper.Map<Recepie>(recepieCreateDto);
- _repository.CreateRecepieObject(recepieModel);
- _repository.SaveChange();
- var recepieReadDto = _mapper.Map<RecepieReadDto>(recepieModel);
- return CreatedAtRoute(nameof(GetRecepieByID), new { Id = recepieReadDto.ID }, recepieReadDto);
-
- }
DbContext :
- public class FoodpackContext : DbContext
- {
- public FoodpackContext(DbContextOptions<FoodpackContext> opt):base(opt)
- {
- }
- public DbSet<Recepie> Recepies { get; set; }