4
Answers

string value is saving as null in database

Istudent Rana

Istudent Rana

7y
201
1
Data type of column in database table is nvarchar(max) and allow null is checked.
 
This is my code.
 
This is my ajax post request.
$.ajax({
url: "/Home/SaveAnswer",
type: 'POST',
data: {
"ProjectId": "1",
"QuestionId": qId,
"Answer": text + ',' + rate,
},
cache: false,
}).done(function (response) {
console.log(response);

}).fail(function (e) {
console.log(e);
});
});

Code in controller

[HttpPost]
public string SaveAnswer(AnswerVM model)
{
string msg = "";
try
{
using(MyDbContext context = new MyDbContext())
{
Answer ans = null;
if(ModelState.IsValid)
{

ans = context.Answers.FirstOrDefault(a => a.ProjectId == model.ProjectId && a.QuestionId == model.QuestionId);
if(ans != null)
{
ans.Answer1 = model.Answer;
msg = "ans is not null - {" + ans.Answer1 + "}";
}
else
{
ans = new Answer();
ans.Answer1 = model.Answer;
ans.ProjectId = model.ProjectId;
ans.QuestionId = model.QuestionId;
context.Answers.Add(ans);

msg = "ans is null - {" + ans.Answer1 + "}";
}
context.SaveChanges();
}
}

return msg;
}
catch(System.Exception ex)
{
return ex.Message;
}
}
 
Answers (4)
0
Istudent Rana

Istudent Rana

NA 18 777 7y
For some reason i my debugging tool is not working. I have been going through this issue.
Unable to start debugging on the web server. Operation not supported.
Unknown error: 0x80004005.
 
If you looked at the ajax code, I have getting back the response as it has to be saved in database.
 
My Sql Profiler report is :
exec sp_executesql N'INSERT [dbo].[Answers]([ProjectID], [SubQuestionID], [QuestionID], [Answer])
VALUES (@0, @1, @2, @3)
SELECT [ID]
FROM [dbo].[Answers]
WHERE @@ROWCOUNT > 0 AND [ID] = scope_identity()',N'@0 int,@1 int,@2 int,@3 nvarchar(max),@0=1559,@1=280,@2=800,@3=N'Completed,10'
 
 
0
Mangesh Gaherwar

Mangesh Gaherwar

NA 4.1k 70.8k 7y
debug ur SaveAnswer () and check there if the value u are passing is present at the
 
AnswerVM model  and its answer property
0
Istudent Rana

Istudent Rana

NA 18 777 7y
How do i do it?
0
Mangesh Gaherwar

Mangesh Gaherwar

NA 4.1k 70.8k 7y
check if the value is getting deserialized properly when it comes to the Controller