Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Can any one please help me with MVC code, situation is I have a question and it has 4 options out of which one is correct .

Eg
1. What is 2 +2 ?
a) 2
b) 3
c) 4
d) 5

Save Next Close


in post how do i get question text , correct option ID, and selected option ID.

please help me

What I have tried:

C#
var evalVM = new Evaluation();
var q1 = new QuestionVM { ID = 1, QuestionText = "What is your favourite language" };
q1.OptionVMs.Add(new OptionVM { ID = 12, AnswerText = "PHP" });
q1.OptionVMs.Add(new OptionVM { ID = 13, AnswerText = "ASP.NET" });
q1.OptionVMs.Add(new OptionVM { ID = 14, AnswerText = "Java" });
evalVM.QuestionsVMs.Add(q1);

var q2 = new QuestionVM { ID = 2, QuestionText = "What is your favourite DB" };
q2.OptionVMs.Add(new OptionVM { ID = 16, AnswerText = "SQL Server" });
q2.OptionVMs.Add(new OptionVM { ID = 17, AnswerText = "MyQL" });
q2.OptionVMs.Add(new OptionVM { ID = 18, AnswerText = "Oracle" });
evalVM.QuestionsVMs.Add(q2);

return View(evalVM);
Posted
Updated 9-Mar-16 5:10am
v2
Comments
John C Rayan 9-Mar-16 7:39am    
What have you tried so far in View and Controller? You have to post your view and controller and by the way you don't have to submit the correct option ID from view. It has to come from server to compare the selected Option

1 solution

Hi Rahul Yadav,
Seems your question needs more details.
1. Are you loading data from any DB?
2. How you are giving a control to the user to select the answer?

If it is a Radio Button list While on click of Post button you can add a click event in JavaScript and you can read the selected option. From JavaScript function you can call controller and pass the data.

In that method you can get the answer by passing QuestionNo to DB.

Here my Example with a Check Box list (as i also don't have much idea about MVC):

$("#PostAnswer").click(function () {  //<pre>PostAnswer is Button ID

        var questionlist = [], counter = 0;
        
        $('.chkbox1').each(function () {
            if ($(this).is(':checked')) {
               var questionID = evalVM[0].ID;
                var id = $(this).attr('ID');
                var question= {
                    QuestionID: questionID,
                    answerID  : id 
                };
                questionlist .push(question);
            }
        });</pre>

 Then you can call the controller by passing the list

var url = baseurl + 'EvaluationController/ValidateAnswers
 $.ajax({
        url: url,
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(questionlist ),
        contentType: 'application/json; charset=utf-8',
        success: function (result) {
            if (result > 0) {
                alert('');
            }
        }
    });

[HttpPost]
        public JsonResult ValidateAnswers(List<evalmodel> evalVM= null)

From controller you can validate against DB.

Sorry If anything is wrong. Even am also new to MVC, But i explained how we are using in our current project.</evalmodel>
 
Share this answer
 

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