all works fine in front end side , all with values and all well but wont arrive as class data and if i send a string in body they will arrive regular and work and method get all works i dont know what happening to this
this in C# asp.net
[HttpPost]
[Route("api/NewPost/Add")]
public IHttpActionResult addPost([FromBody] Post newPost)
{
try
{
string str = $"INSERT INTO [Post]([userId],[title], [description], [photos], [catalogType],[postDate],[typePost]) VALUES({newPost.userId},'{newPost.title}','{newPost.description}','{newPost.photos}','{newPost.catalogType}','{newPost.postDate}','{newPost.typePost}')";
SqlConnection con = connectionDb.connectToDb();
SqlCommand comm = new SqlCommand(str, con);
con.Open();
comm.ExecuteReader();
con.Close();
return Ok(true);
}
catch (Exception err)
{
return Ok(err.Message);
}
return Ok(false);
}
here what in js and all with value
fetch('http://localhost:55866/api/NewPost/Add',{
method:'POST',
headers:{
"Content-Type":"application/json"
},
body:JSON.stringify({postId:-1,userId,title,description,photos,catalogType:route.params.catalog,postDate:null,typePost})
}).then(r=>r.json()).then(data=>{
if(data === true){
setAlertSucessfully(true)
}
}
).catch(err=>console.log('error :' + err.message))
here the class i tried to public all and wont work too arrive as null with problem
-
$exception {"Object reference not set to an instance of an object."} System.NullReferenceException
public class Post
{
// automatic post Id
public int postId;
public int userId;
public string title;
public string description;
public string photos;
public string catalogType; //dog - cat - other
public DateTime postDate;
public string typePost; //advice - ask - photo
```
public Post(int postId,int userId, string title, string description, string photos, string catalogType, DateTime postDate, string typePost)
{
if (postId != -1)
this.postId = postId;
else
this.postId = -1;
Console.WriteLine(postId);
this.userId = userId;
this.title = title;
this.description = description;
if (photos == null)
this.photos = null;
else
this.photos = photos;
this.typePost = typePost;
this.catalogType = catalogType;
this.postDate = DateTime.Now;
}
public override string ToString()
{
return $"{postId} {userId} {title} {description} {photos} {typePost} {catalogType} {postDate}";
}
}