TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
ehsan khosravi
NA
20
3.3k
how to deserialize json array in asp.net core api in post method?
Jan 6 2021 6:41 PM
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
; }
// include ingredientName / value / measure array
[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 int MentorID { 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 :
//Post api/recepie
[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);
// return Ok(recepieModel);
}
DbContext :
public
class
FoodpackContext : DbContext
{
public
FoodpackContext(DbContextOptions<FoodpackContext> opt):
base
(opt)
{
}
public
DbSet<Recepie> Recepies {
get
;
set
; }
Reply
Answers (
5
)
Open file upload on profile click in asp. Net
how to resolve contextswitchdeadlock error ?