Search code examples
c#asp.net-coredrop-down-menudropdownlistfor

Creating a DropDownList reading directly from a table (ASP.NET MVC CORE & c#)


Need a little help in here.

I want to create a drop down list and be able to display all the different values of a single row (in this case, a dropdown list containing emails); i am reading directly from a SQL SERVER table:

I created a Model cs file called DropDownList and it contains:

public class Email{
    public string Email { get; set}

}

I did this function in order to get all the values stored in that table:

public IEnumerable<Emails> GetEmailList()
{
    var emailList = new List<Emails>();

    using (SqlConnection con = new SqlConnection(connectionString))
    {

        SqlCommand cmd = new SqlCommand("GetEmailValues", con);
        cmd.CommandType = CommandType.StoredProcedure;
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {

            var listEmail = new Emails();
            listEmail.Email = dr["Email"].ToString();

            emailList.Add(listEmail);
        }
        con.Close();
    }
    return emailList;
}

This is my controller file:

public ActionResult Index()
{
    List<Emails> emailList = dbContext.GetEmailList().ToList();
    return View(emailList);
}

This is my HTML file:

@model Application.Models

@{
    ViewData["Title"] = "Edit";
}

<h1>EDIT</h1>

<h4>Edit</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Edit">
            <div class="form-group">
                <label asp-for="Email" class="control-label"></label>
                <input asp-for="Email" class="form-control" />
                <span asp-validation-for="Email" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Save" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

I do not understand what should i do in order to get that DROPDOWNLIST, been reading different things but no one really helped me. Can help me?


EDIT:

I would like to obtain something like this:

image


Solution

  • You can use asp-items taghelper:

    public ActionResult Edit()
    {
      ViewData["Email1"] = new SelectList(dbContext.GetEmailList().ToList(), "Email", "Email");
       ViewData["Email2"] = new SelectList(//your selected list ,"Email", "Email");
      return View();
    }
    [HttpPost]
        public IActionResult Edit(string email1,string email2)
        {
            //your code.
            return View();
        }
    

    Edit View:

    @model DropDownList
    
    <div class="row">
        <div class="col-md-4">
            <form asp-action="Edit">
                <div class="form-group">
                        <select name="email1" asp-items="ViewBag.Email1"></select>
                    </div>
                    <div class="form-group">
                        <select name="email2" asp-items="ViewBag.Email2"></select>
                    </div>
            </form>
        </div>
    </div>
    

    enter image description here