First of all, the code below seems a bit redundant to me
var now = DateTime.Now;
var date = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
wouldn't it be better to just have this line?
DateTime date = DateTime.Now;
Maybe I am missing something, but I fail to see how you will save more than one result using this code
foreach (QuestionsModel ans in model)
{
obj.TestID = ans.TestId;
obj.SkillID = ans.SkillId;
obj.QuestionID = ans.ID;
obj.CandidateAns = ans.SelectedAnswer;
obj.TestDate = date;
}
For each iteration of the loop, you change the properties of the single instance
obj
Shouldn't this be an array?
As for the error you get.
Have you tried to skip the loop and just save one row?
As you don't show your table structure, I can only guess that
ans.ID
is a foreign key and this ID doesn't exist.