Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need dropdown list of my class tablein  my index view when i use viewbag method but it still show error in my index view ClassID,how i can remove this issue plz some one help me.. 


What I have tried:

THIS IS MY iNDEX VIEW.

@using PagedList;
@using PagedList.Mvc;
@model PagedList.IPagedList<opr.Data.C_QuestionTable>
@model opr.Data.myclass
@{
    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., (SelectList)ViewBag.dataForDropDowns, new { @class = "form-control select2ddl" })
    </div>
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    

</div>


this my controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using opr.Data;
using PagedList;
using PagedList.Mvc;

namespace opr.Controllers
{
    public class OnlineQuizeController : Controller
    {
        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;

            List<object> myobject = new List<object>();
            myobject.Add(db.myclasses.ToList());
            ViewBag.classIDs = myobject;
            var pageNumber = page ?? 1;
            var pageSize = 3;
            var question = db.C_QuestionTable.OrderBy(x => x.QuestionText).ToPagedList(pageNumber, pageSize);   
            return View(question);
           
        }

       }
}
Posted
Updated 19-Mar-18 22:37pm

1 solution

It looks like your model object would be single object for this view but you are binding your view with IPagedList<Popr.Data.C_QuestionTable> which is a collection not single item.

So, you need to set your model to be :

C#
@model Popr.Data.C_QuestionTable


and now you should be able to access the property :

C#
<pre> @Html.DropDownListFor(model => model.ClassID, (SelectList)ViewBag.dataForDropDowns, new { @class = "form-control select2ddl" })
 
Share this answer
 
v3

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