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
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();
}
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
411 Points
1327 Posts
Set selected value of SelectList Item if only exist in another list<int> variable
Jan 17, 2021 11:33 PM|polachan|LINK
Hi
I am trying to set some values to have selected as default in a multiple selectlist. Here is my code.
my Repository
view
Controller
With Thanks
Pol
All-Star
53041 Points
23612 Posts
Re: Set selected value of SelectList Item if only exist in another list<int> variable
Jan 18, 2021 01:24 AM|mgebhard|LINK
You did not share where "depotNo" is set.
Member
330 Points
235 Posts
Re: Set selected value of SelectList Item if only exist in another list<int> variable
Jan 18, 2021 03:20 AM|SurferOnWww|LINK
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
Contributor
2730 Points
778 Posts
Re: Set selected value of SelectList Item if only exist in another list<int> variable
Jan 19, 2021 08:33 AM|YihuiSun|LINK
Hi polachan,
You can use MultiSelectList and set the fourth parameter. I wrote an example, you can refer to it.
Model
Controller
View
Here is the result.
Best Regards,
YihuiSun