mardi 4 avril 2017

Bind values from database to dropdown in MVC with Entity frame work

I am trying to bind a value from database to the dropdown in 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.

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

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..

Aucun commentaire:

Enregistrer un commentaire