c#
Add Comment below post [on hold]
I have problem. I want to add comment below my post (via ajax) but I can't get postID. I have always value = null when I want to insert to database and return error ("empty value postId"). What Am I doing wrong? Any suggestion. Sorry for my English. My code: [HttpPost] public string AddComment(string commentName, string commentBody) { //Declare id string id; using (Db db = new Db()) { CommentDTO dto = new CommentDTO(); dto.Name = commentName; dto.Body = commentBody; dto.CreatedAt = DateTime.Now; BlogDTO postDTO = db.Blog.FirstOrDefault(x => x.Id == dto.PostId); dto.PostId = postDTO.Id; db.Comments.Add(dto); db.SaveChanges(); id = dto.Id.ToString(); } return id; }
You are looking for postDTO, by dto.PostId which is null or 0, because you have just created that object (dto), assigned comment, body and date, but not rest of properties. You need to pass postId to controller and then to find post by that Id.
In your code, i couldn't understand how you get value of dto.PostId and in next statement you are assigning a value to dto.PostId by using postDTO.Id and you get value for postDTO by using dto.PostId First you need a value of PostId, which you can pass to you post method either using model object as parameter (also pass other values in this model object) or just a string parameter. then use this postId to get value from database or to save in to database.
Related Links
CustomerId and Request.QueryString in ASP.NET
How do I convert int to a double?
How to find an object from a List of objects?
“Insufficient resources to perform operation.” MSMQ when transaction contains multiple messages
Perform a Linq Many to Many Query
How to send length of a package via tcp/ip protocol
Relative Path in the executing assembly
give count to links [duplicate]
Where should the System.Collections.Concurrent collections be used?
C# BackGroundWorker with ProgressBar Updates after process complete
How to store/retrieve HTML in MySQL using .Net (C#/VB)
How can I refactor this ForEach(..) code to use Parallel.ForEach(..)?
How to read a specific number from a HTML page [closed]
How does umbraco mix XSLT with C# code
How to cancel TCP file transfer c#
Problem when trying to delete row from DataTable