locked
Set selected value of SelectList Item if only exist in another list<int> variable RRS feed

  • Question

  • User-1355965324 posted

    Hi

    I am trying to set some values to have selected  as default  in a  multiple selectlist. Here is my code.

    my Repository 

     public IEnumerable<SelectListItem> GetLocations(int depotNo)
            {
    
                List<SelectListItem> locations = (from dpt in ctx.Department                                           
                                                  select new SelectListItem
                                                  {
                                                      Value = dpt.DepartmentID.ToString(),
                                                      Text = dpt.DepartmentName,
                                                      Selected = user.DepotNo == depotNo 
                                                  }).ToList<SelectListItem>();
                locations.Insert(0, new SelectListItem
                {
                    Value = "0",
                    Text = "-- Select --"
                });
                return locations;

    view

    <div class="form-group">
         <label class="control-label control-label-left col-sm-3 text-danger" for="field2">Department*</label>
         <div class="controls col-sm-9">
              <select id="dropdownDepartment" class="form-control" multiple asp-for="Department" 
    		  asp-items="@ViewBag.UserDepartments" data-role="select">
              </select><span id="errId2" class="error"></span>
         </div>
    </div>

     Controller

    List<int> numbers = new List<int> 
    numbers.Add(30);  
    numbers.Add(35);  
    numbers.Add(40);
    List<SelectListItem> dept = new List<SelectListItem>();
    dept = ctx.UserDepartmentLink.GetLocations(depotno).ToList();
    Here also I have to add more selected value for dropdownDepartment if there is any dept value exist from numbers list int variable ViewBag.UserDepartments = dept;
    How can I add as selected item if there is any value hold as same value in number list int variable

    With Thanks

    Pol

    Sunday, January 17, 2021 11:33 PM

All replies

  • User475983607 posted

    You did not share where "depotNo" is set.

    Monday, January 18, 2021 1:24 AM
  • User-1545767719 posted

    I am trying to set some values to have selected  as default  in a  multiple selectlist. Here is my code.

    See my reply in another thread you posted:

    https://forums.asp.net/t/2173715.aspx?Get+Selected+Value+from+the+multiple+SelectList+from+the+view+in+to+the+Controller

    Monday, January 18, 2021 3:20 AM
  • User1686398519 posted

    Hi polachan, 

    You can use MultiSelectList and set the fourth parameter. I wrote an example, you can refer to it.

    Model

        public class TestMultiSelectListViewModel
        {
            public string Department { get; set; }
        }
        public class Department
        {
            [Key]
            public int DepartmentID { get; set; }
            public string DepartmentName { get; set; }
        }

    Controller

    public IActionResult Index()
    {
        List<int> numbers = new List<int>();
        numbers.Add(1);
        numbers.Add(5);
        numbers.Add(3);
        MultiSelectList test = new MultiSelectList(db.Departments.ToList(),"DepartmentID", "DepartmentName",numbers);
        ViewBag.UserDepartments = test;
        return View();
    }

    View

    @model DailyCoreMVCDemo.Models.TestMultiSelectListViewModel
    <select asp-for="Department" asp-items="@ViewBag.UserDepartments" size="3" multiple class="form-control"></select>

    Here is the result. 

    Best Regards,

    YihuiSun

    Tuesday, January 19, 2021 8:33 AM