使用实体框架工作将数据库中的值绑定到MVC中的下拉列表

时间:2022-02-19 02:34:22

I am trying to bind a value from database to the dropdown in View.

我试图将数据库中的值绑定到View中的下拉列表。

I have a requirement that I need to display database values in dropdown list in a view. I am trying to bind the Email id to the dropdown from the database table.

我要求我需要在视图的下拉列表中显示数据库值。我试图将电子邮件ID绑定到数据库表的下拉列表。

Here I need to add the Email ID to the dropdown. This is my Model,

在这里,我需要将电子邮件ID添加到下拉列表中。这是我的模特,

public class UserDetails
{
      public List<SelectListItem> Userdetails { get; set; }
      public string Email { get; set; }
}

This is My Controller,

这是我的控制器,

public ActionResult Admin()
{
            var db = new DataEntities1();
            var query = db.tblEmployees.Select(c => new { c.Email });
            ViewBag.Categories = new SelectList(query.AsEnumerable(), "CategoryID", "CategoryName");
            return View();
}

This is my View,

这是我的观点,

   @model List<MVC_Sample.Models.UserDetails>

             <div class="dropdown">
               @Html.DropDownList("CategoryID", (SelectList)ViewBag.Categories, "--Select One--")

                </div>

View will be loaded with dropdown and the values from database. But unfortunately, I am not able to add the values to drop down. I dono where I did the wrong here. Can anyone help me on this..

视图将加载下拉列表和数据库中的值。但遗憾的是,我无法将值添加到下拉列表中。我不知道我在哪里做错了。谁可以帮我这个事..

1 个解决方案

#1


1  

As other users coment you on the comments, the problems may be, that CategoryID and CategoryName are not present on thq query

正如其他用户对评论的注意,问题可能是,查询中不存在CategoryID和CategoryName

However, I usually prefer to bind the dropdown to List<SelectListItem> instead of bind it to a SelectList, because it make easier to create a strong typed query.

但是,我通常更喜欢将下拉列表绑定到List ,而不是将其绑定到SelectList,因为它更容易创建强类型查询。

I would change these lines on the controller:

我会在控制器上更改这些行:

var query = db.tblEmployees.Select(c => new { c.Email });
ViewBag.Categories = new SelectList(query.AsEnumerable(), "CategoryID", "CategoryName");

To:

List<SelectListItem> query = db.tblEmployees.Select(c => new SelectListItem { Text = c.Email, Value = c.CategoryId }).toList();
ViewBag.Categories = query;

Then, in the view. you should change

然后,在视图中。你应该改变

@Html.DropDownList("CategoryID", (SelectList)ViewBag.Categories, "--Select One--")

To:

@Html.DropDownList("CategoryID", (List<SelectListItem>)ViewBag.Categories, "--Select One--")

#1


1  

As other users coment you on the comments, the problems may be, that CategoryID and CategoryName are not present on thq query

正如其他用户对评论的注意,问题可能是,查询中不存在CategoryID和CategoryName

However, I usually prefer to bind the dropdown to List<SelectListItem> instead of bind it to a SelectList, because it make easier to create a strong typed query.

但是,我通常更喜欢将下拉列表绑定到List ,而不是将其绑定到SelectList,因为它更容易创建强类型查询。

I would change these lines on the controller:

我会在控制器上更改这些行:

var query = db.tblEmployees.Select(c => new { c.Email });
ViewBag.Categories = new SelectList(query.AsEnumerable(), "CategoryID", "CategoryName");

To:

List<SelectListItem> query = db.tblEmployees.Select(c => new SelectListItem { Text = c.Email, Value = c.CategoryId }).toList();
ViewBag.Categories = query;

Then, in the view. you should change

然后,在视图中。你应该改变

@Html.DropDownList("CategoryID", (SelectList)ViewBag.Categories, "--Select One--")

To:

@Html.DropDownList("CategoryID", (List<SelectListItem>)ViewBag.Categories, "--Select One--")