绑定MVC中的下拉列表 [英] Bind the Dropdown list in MVC

查看:82
本文介绍了绑定MVC中的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我是MVC新手。

我试图绑定下拉列表。但它给了我以下错误:

Hello,

I am new in MVC.
I am trying to bind the dropdownlist. But its giving me the following error:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.




My Controller code is as follow :

public ActionResult GetAllDepartment()
        {
            using (var db = new TestEntities())
            {
                IEnumerable<SelectListItem> items = db.DepartmentMasters.Select(c => new SelectListItem
                {
                    Value = c.DepartmentID.ToString(),
                    Text = c.DepartmentName
                });
                ViewBag.DepartmentMaster = items;
               
               return View();
            }
            
        }





查看代码如下:





View code is as follow :

@model MvcApplication4.Models.DepartmentMaster

@{
    ViewBag.Title = "GetAllDepartment";
}

<h2>GetAllDepartment</h2>
@using (Html.BeginForm())
{
    @Html.DropDownList("DepartmentID", (IEnumerable<SelectListItem>)ViewBag.DepartmentMaster)
   
   
}





你能帮我解决一下代码中的问题吗?



can you please help me where is the problem in my code?

推荐答案

您可以制作一个部门列表以避免处理上下文。

You could make a list of departments to avoid the disposed context.
using (var db = new TestEntities())
         {


         List<department> DepList = db.DepartmentMaster.AsEnumerable<department>().ToList();
         IEnumerable<SelectListItem> items = DepList.Select(c => new SelectListItem
            {
                Value = c.DepartmentID.ToString(),
                Text = c.DepartmentName
            });

         ViewBag.DepartmentMaster = items;



         return View();
        }


看起来你收到了错误,因为它包含在using语句中并且你传递了值通过ViewBag。您应该为此创建一个模型并使用模型将其传递给视图。



模型

It looks like you are getting the disposed error because its wrapped inside of a using statement and you are passing the value through the ViewBag. You should really create a model for this and pass it using the model to the view.

Model
public Class MyModel
{
    public SelectListItem DepartmentMaster { get; set; }
}







控制器






Controller

public ActionResult GetAllDepartment()
        {
            using (var db = new TestEntities())
            {
                MyModel model = new Model();
                IEnumerable<SelectListItem> items = db.DepartmentMasters.Select(c => new SelectListItem
                {
                    Value = c.DepartmentID.ToString(),
                    Text = c.DepartmentName
                });
                model.DepartmentMaster = items;
               
               return View(model);
            }
            
        }





型号





Model

@model MvcApplication4.Models.DepartmentMaster
 
@{
    ViewBag.Title = "GetAllDepartment";
}
 
<h2>GetAllDepartment</h2>
@using (Html.BeginForm())
{
    @Html.DropDownList("DepartmentID", Model.DepartmentMaster)


这篇关于绑定MVC中的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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