locked
Populating Select from Database RRS feed

  • Question

  • User1122355199 posted

    Hello everyone and thanks for the help in advance.  This is probably a stupid question, but I can't seem to figure out how to first populate a select tag from a List <T> and also set the selected option.  I can't seem to find an example that doesn't manually set each list item.  Any help would be appreciated.

    Thursday, November 29, 2018 2:13 AM

Answers

  • User-552477072 posted

    hi kmcnet,

    Your viewModel code will be like this: 


    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Web.Mvc;
    
    namespace Project01.ViewModels
    {
        public class AAViewModel
        {
           
            [Required]
            [Display(Name = "Country")]
            public string SelectedCountry { get; set; }
            public IEnumerable<SelectListItem> Countries { get; set; }
    
        }
    }
    The field storing the selected item holds the unique key for each entity. The unique key of the selected value will be the same 
    as one of the elements of the 
    SelectListItem.

    Rezor View:
    @Html.DropDownListFor(x => Model.SelectedCountry, new SelectList(Model.Countries, "Value", "Text"), htmlAttributes: new { @class = "form-control", id = "Country"})
    Please remember to "Mark as Answer" if suggestion helps.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 29, 2018 2:45 AM
  • User1520731567 posted

    Hi kmcnet,

    According to your requirement,I make a simple demo,you could refer to it:

    Controller:

    public class LabelModel
    {
        public int LabelId { get; set; }
        public string LabelName { get; set; }
    }
    
    public ActionResult Index()
    {
        var options = new List<LabelModel>();
    
        options.Add(new LabelModel() { LabelId = 11, LabelName = "New Request" });
        options.Add(new LabelModel() { LabelId = 12, LabelName = "Reviewed" });
        options.Add(new LabelModel() { LabelId = 13, LabelName = "Started" });
        options.Add(new LabelModel() { LabelId = 14, LabelName = "In Progress" });
        options.Add(new LabelModel() { LabelId = 15, LabelName = "Completed" });
    //public SelectList(IEnumerable items, string dataValueField, string dataTextField, object selectedValue); ViewBag.ddl = new SelectList(options, "LabelId", "LabelName", options[1].LabelId); //options is List<T> return View(); }

    In View:

    @Html.DropDownList("ddl", null, "----Select----", new { @class = "form-control"})
    

    How my demo works:

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 30, 2018 7:35 AM

All replies

  • User-552477072 posted

    hi kmcnet,

    Your viewModel code will be like this: 


    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Web.Mvc;
    
    namespace Project01.ViewModels
    {
        public class AAViewModel
        {
           
            [Required]
            [Display(Name = "Country")]
            public string SelectedCountry { get; set; }
            public IEnumerable<SelectListItem> Countries { get; set; }
    
        }
    }
    The field storing the selected item holds the unique key for each entity. The unique key of the selected value will be the same 
    as one of the elements of the 
    SelectListItem.

    Rezor View:
    @Html.DropDownListFor(x => Model.SelectedCountry, new SelectList(Model.Countries, "Value", "Text"), htmlAttributes: new { @class = "form-control", id = "Country"})
    Please remember to "Mark as Answer" if suggestion helps.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 29, 2018 2:45 AM
  • User1520731567 posted

    Hi kmcnet,

    According to your requirement,I make a simple demo,you could refer to it:

    Controller:

    public class LabelModel
    {
        public int LabelId { get; set; }
        public string LabelName { get; set; }
    }
    
    public ActionResult Index()
    {
        var options = new List<LabelModel>();
    
        options.Add(new LabelModel() { LabelId = 11, LabelName = "New Request" });
        options.Add(new LabelModel() { LabelId = 12, LabelName = "Reviewed" });
        options.Add(new LabelModel() { LabelId = 13, LabelName = "Started" });
        options.Add(new LabelModel() { LabelId = 14, LabelName = "In Progress" });
        options.Add(new LabelModel() { LabelId = 15, LabelName = "Completed" });
    //public SelectList(IEnumerable items, string dataValueField, string dataTextField, object selectedValue); ViewBag.ddl = new SelectList(options, "LabelId", "LabelName", options[1].LabelId); //options is List<T> return View(); }

    In View:

    @Html.DropDownList("ddl", null, "----Select----", new { @class = "form-control"})
    

    How my demo works:

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 30, 2018 7:35 AM
  • User1122355199 posted

    Thank you both for the help.  I appreciate it.

    Tuesday, December 4, 2018 10:35 PM