Dear all,
I have the following class which needs to display questions to the user based on test id. Next, i am stuck for LOOP part that how to assign "i" inside model in view model part and prompt error message: Cannot applying indexing with [] to an expression of type "MutliEntityModel" when try to define loop on every model view.
below is my coding of view part.
@model ABC.Models.MultiEntityModel
{
.....................................
@{ for (int i = 0; i < maxQid; i++)
{
// i am stuck for loop part that how to assign "i" inside Model .<tr><td>@Html.Raw("Q")@Html.Raw(Model[i].QuestionModel.Question_Id?? new int { }) </td><br /></tr><tr><td>@Html.Raw(Model[i].QuestionModel.Question_Title ?? "")</td></tr><tr>
@if (Model[i].QuestionModel.Question_ImagePath != null)
{
<td><img src="~/Images/Uploads/@Model[i].QuestionModel.Question_ImageName" alt="@Model[i].QuestionModel.Question_ImageName" width="100" height="100" /></td>
}</tr><tr><td><br /></td></tr><tr><td>
@Html.RadioButtonFor(model => model[i].ChoiceModel.Choice_CandidateIsSelected, 1)
@Html.DisplayFor(model => model[i].QuestionModel.Question_ans1)</td></tr><tr><td>
@Html.RadioButtonFor(model => model[i].ChoiceModel.Choice_CandidateIsSelected, 2)
@Html.DisplayFor(model => model[i].QuestionModel.Question_ans2)</td></tr>
if (Model[i].QuestionModel.Question_ans3 != null)
{
<tr><td>
@Html.RadioButtonFor(model => model[i].ChoiceModel.Choice_CandidateIsSelected, 3)
@Html.DisplayFor(model => model[i].QuestionModel.Question_ans3)</td></tr>
}
if (Model[i].QuestionModel.Question_ans4 != null)
{<tr><td>
@Html.RadioButtonFor(model => model[i].ChoiceModel.Choice_CandidateIsSelected, 4)
@Html.DisplayFor(model => model[i].QuestionModel.Question_ans4)</td></tr>
}
<tr><td><br /><br /></td></tr>
}
}
}coding in controller :
public ActionResult QuestionPage(string Test_Id, int? id)
{
if (Session["submit"] == null)
{
Session["submit"] = false;
// pass choice id
}
if ((bool)Session["submit"] == true)
{
return RedirectToAction("Create", "Result");
}
else
{
Test_Id = Session["Test_Id"].ToString();
string maxQid = context.Questions.Where(m => m.Test_Id == Test_Id).Select(m => m.Question_Id).DefaultIfEmpty(0).Max().ToString();
ViewBag.maxQuestionId = int.Parse(maxQid);
MultiEntityModel _Rmodel = null;
_Rmodel = new MultiEntityModel ();
_Rmodel.QuestionModel = context.Questions.Where(x => x.Question_Id == id && x.Test_Id == Test_Id).SingleOrDefault();
return View(_Rmodel);
}
}Can anybody help me to solve this issue. Thanks in Advance.