I'm trying to include my class model in the index view so that I can have ClassID for a dropdown menu displaying classes, but I'm unsure of how to do it.
This is what my index looks like:
@using opr.Models;
@using PagedList;
@using PagedList.Mvc;
@model PagedList.IPagedList<opr.Data.C_QuestionTable>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div class="alert-success form-control m-auto w-75 custom-form">
<div class="col-6 pb-3 input-control">
<label for="ex3"> Select Class:</label>
@Html.DropDownListFor(model => model.ClassID, (SelectList)ViewBag.dataForDropDowns, new { @class = "form-control select2ddl" })
</div>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
QuestionText
</th>
</tr>
@foreach (var item in Model)
{
foreach (var answer in item.C_AnswerTable)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.QuestionText)
@Html.RadioButton("searchBy", answer.Options, true)
<text>@answer.Options</text>
</td>
</tr>
}
}
<tr>
<div class="pagination pull-left">
<hr />
<td colspan="3">@Html.PagedListPager(Model, page => Url.Action("Index", new { page = page }))</td>
</div>
</tr>
</table>
</div>
Here is my Controller code:
examsEntities db = new examsEntities();
public ActionResult Index(int?page)
{
var mod1 = db.myclasses.Select(s => new { s.ID, s.C_name }).ToList();
SelectList sList = new SelectList(mod1, "ID", "C_name");
ViewBag.dataForDropDown = sList;
var pageNumber = page ?? 1;
var pageSize = 3;
var question = db.C_QuestionTable.OrderBy(x => x.QuestionText).ToPagedList(pageNumber, pageSize);
return View(question);
}