我想在索引视图中获取classid。 [英] I want to get classid in my index view.

查看:95
本文介绍了我想在索引视图中获取classid。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);
           
        }

       }
}

推荐答案

看起来像您的模型对象将是此视图的单个对象,但您将视图绑定到 IPagedList< Popr.Data.C_QuestionTable> 这是一个集合而不是单个项目。



因此,您需要将您的模型设置为:



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 :

@model Popr.Data.C_QuestionTable





现在你应该可以进入酒店了:





and now you should be able to access the property :

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


这篇关于我想在索引视图中获取classid。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆