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

Categories

HOME
qlikview
actionscript-3
indexing
optimization
stata
azure-active-directory
stdout
squeak
thermal-printer
glympse
atlassian-fisheye
schema
sahi
normals
olap
apache-httpclient-4.x
fastreport
javascript-debugger
console-application
responsive
jsfiddle
amazon-data-pipeline
css-float
beacon
gomobile
monad-transformers
tcpdf
uipath
windowserror
ckeditor4.x
datanucleus
nsopenpanel
libxml2
custom-fields
hierarchy
spring-data-gemfire
2checkout
intermec
qt-designer
eclipse-jdt
pymunk
rythm
jvisualvm
mef
rivets.js
remap
lucee
e4
try-catch-finally
spring-social-twitter
addressbook
joystick
disconnect
require-handlebars
sprockets
php-java-bridge
django-taggit
camellia
periodic-processing
oryx
random-sample
tess4j
textblob
cakephp-2.6
pymol
xmla
chunks
backtrace
information-hiding
grunt-contrib-connect
qf-test
tigase
mvcgrid
xpand
hotlinking
new-operator
elixir-framework
iwork
mongoid4
ardor3d
xrandr
forward-declaration
latex-suite
system.transactions
antixsslibrary
zepto
moonscript
linear-interpolation
esi
nude.js
scsf
visual-leak-detector
isgl3d
pex-and-moles
jquery-click-event
youtube.net-api
lwp
jettison
stackless

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile