Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
This is my code and i want the every question and selected radio button answer need to be save into database. While posting the data to the controller values become null.. Please suggest me..

If you have any solution please follow the test sample project link here:
dropbox.com/s/v33xcy3c2x72keo/TestQuestions.rar?dl=0

Please help me to resolve the solution
Thanks in Advance.

Model :
C#
public class Question
{
    public decimal TestId { get; set; }
    public decimal SkillId { get; set; }
    public decimal ID { get; set; }
    public string QuestionText { get; set; }
    public List<OptionList> Options { set; get; }        
}

public class OptionList
{
    public string Option1 { get; set; }
    public string Option2 { get; set; }
    public string Option3 { get; set; }
    public string Option4 { get; set; }
}

Controller :
C#
public ActionResult Successpage()
{
   decimal testid = 1;
   var items = (from p in db.ALBulkTestDetails
                where p.TestID == testid
                select new Question
                {
                    ID = p.QuestionID,
                    QuestionText = p.Question,
                    SelectedAnswer = null,
                    Options = (from a in db.ALBulkTestDetails
                               join b in db.ALBulkTestDetails
                               on new { aa = a.QuestionID, a.SkillID } equals new { aa = b.QuestionID, b.SkillID }
                              where b.QuestionID == p.QuestionID &amp;&amp; b.SkillID == p.SkillID
                               select new OptionList
                               {
                                   Option1 = b.Option1,
                                   Option2 = b.Option2,
                                   Option3 = b.Option3,
                                   Option4 = b.Option4
                               }).ToList()
                }).Take(3).ToList();
   return View(items);
}
Posted
Updated 27-Nov-15 1:36am
v4
Comments
Anil Sharma1983 27-Nov-15 7:07am    
Please check radio button name posted values your form
jagan12013 27-Nov-15 7:14am    
Tell me according model class and controller how to design in a view for posting submitted answers.

1 solution

Step1: Define Question and Option model
C#
public class Question
{
    public decimal TestId { get; set; }
    public decimal SkillId { get; set; }
    public decimal ID { get; set; }
    public string QuestionText { get; set; }
    public List<OptionList> Options { set; get; }
	public string SelectedAnswer { get; set; }	
}

public class OptionList
{
    public string Option1 { get; set; }
    public string Option2 { get; set; }
    public string Option3 { get; set; }
    public string Option4 { get; set; }
}

Step2: Design View page and add your viewmodel top of view page.
HTML
@model yourproject.model.Question; // Change as per your requirement
foreach (var temp in Model.Options)
{
	<div>
		@Html.RadioButtonFor(model => Model.SelectedAnswer, "1"})
		@Html.Label(temp.Option1)
	</div>
	<div>
		@Html.RadioButtonFor(model => Model.SelectedAnswer, "2")
		@Html.Label(temp.Option2)
	</div>
	<div>
		@Html.RadioButtonFor(model => Model.SelectedAnswer, "3")
		@Html.Label(temp.Option3)
	</div>
	<div>
		@Html.RadioButtonFor(model => Model.SelectedAnswer, "4")
		@Html.Label(temp.Option4)
	</div>
}

Step3: Add an action in Controller to get value
C#
[HttpPost]
public AcitonResult Index(Question qModel)
{
    if (ModelState.IsValid)
    {
        //TODO: Save your model and redirect 
	string temp = qModel.SelectedAnswer;
    }

    return View();
}
 
Share this answer
 
Comments
jagan12013 29-Nov-15 9:14am    
[HttpPost]
public ActionResult Solution(List Questionsmodel model)
{
if (ModelState.IsValid)
{
ALBulkTestResult obj = new ALBulkTestResult();
var now = DateTime.Now;
var date = new DateTime(now.Year, now.Month, now.Day,
now.Hour, now.Minute, now.Second);
foreach (QuestionsModel ans in model)
{

obj.TestID = ans.TestId;
obj.SkillID = ans.SkillId;
obj.QuestionID = ans.ID;
obj.CandidateAns = ans.SelectedAnswer;
obj.TestDate = date;
}
//db.Entry(obj).State = EntityState.Modified;
db.ALBulkTestResults.Add(obj);
db.SaveChanges();
}
return RedirectToAction("Successfull");
}

this is the right wasy to the save multiple records. Please correct me..
Here i'm getting error
An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code Additional information: An error occurred while updating the entries. See the inner exception for details.
[no name] 29-Nov-15 9:38am    
What is your intention, are you updating records or adding new records?
Secondly make sure that you are getting correct data in model to save in DB. Means all non-null fields should have value in model.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900