As I develop a survey form through the ASP.NET MVC structure, I encounter a phase where I extract the survey questions from a model to the view.
The model I have created is as follows:
[NotMapped]
public class QuizForService
{
public int MainDetailsId { get; set; }
public int QuizID { get; set; }
public string Quiz { get; set; }
public string AnswerForQuiz { get; set; }
}
From the controller, I load the questions into this model:
List<QuizForService> Questionsto = (from q in db.tbl_QuestionsSub
where q.ServiceTypeId == ServiceId
select new QuizForService
{
QuizID = q.Id,
Quiz = q.QuestionEng
}).ToList();
return Json(new {
Success = true,
questionlist = Questionsto,
}, JsonRequestBehavior.AllowGet);
In the view, I iterate through the questions using a loop and provide answer choices for users:
for (int i = 0; i < SurveyList.Count; i++) {
<tr>
<td> @SurveyList[i].Quiz </td>
<td> @if(@SurveyList[i].QuizID != 8) {
@SurveyList[i].AnswerForQuiz <div class="radio">
<input label="Excellent π" type="radio" id="Excellent" name="@SurveyList[i].QuizID" value="Excellent" checked>
<input label="Good π" type="radio" id="Good" name="@SurveyList[i].QuizID" value="Good">
<input label="Fair π" type="radio" id="Fair" name="@SurveyList[i].QuizID" value="Fair">
<input label="Poor πΆ" type="radio" id="Poor" name="@SurveyList[i].QuizID" value="Poor">
</div> } else { <div class="radio"> <
input label="Yes π" type="radio" id="Yes" name="@SurveyList[i].QuizID" value="Yes" checked>
<input label="No π " type="radio" id="No" name="@SurveyList[i].QuizID" value="No">
<input label="Maybe π€" type="radio" id="Maybe" name="@SurveyList[i].QuizID" value="Maybe">
</div> } </td>
</tr>
This view serves as a single-page application that operates in a wizard-like fashion.
My plan involves using JavaScript to fetch the selected values and then pass them to the controller via Ajax.
I seek guidance on retrieving the question IDs along with the selected customer answers in JavaScript.