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.
Code in controller
[HttpPost]
public string SaveAnswer(AnswerVM model)
{
string msg = "";
try
{
using(MyDbContext context = new MyDbContext())
{
Answer ans = null;
$.ajax({
url: "/Home/SaveAnswer",
type: 'POST',
data: {
"ProjectId": "1",
"QuestionId": qId,
"Answer": text + ',' + rate,
},
cache: false,
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);
});
});
}).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);
{
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.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)
{
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;
}
}
}
}
Istudent RanaPosted Jan 22, 2018, 11:39 AM
Unknown error: 0x80004005.
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'
Mangesh GPosted Jan 22, 2018, 11:36 AM
Istudent RanaPosted Jan 22, 2018, 11:34 AM
Mangesh GPosted Jan 22, 2018, 11:11 AM