- Home
- Forum
- ASP.NET MVC - EJ 2
- Selected items in multiselect
Selected items in multiselect
Hello, I have a "Checkbox" multi-select/dropdown list in my Asp.Net MVC 5 application.
I use this code on the Razor page to create the checkbox dropdown. It displays very nicely and I can see my list of "Category".
How do I set the initial selected values. For example, if I have these for my categories:
ID Name
1 Category A
2 Category B
3 Category C
I want to initialize the Multiselect in the Razor page, and have Id=2 (Category B) and Id=3 (Category C) checked when the page is first rendered. I want to do it on the Razor page (server side), and not in Javascript.
I looked to see if there was a "SelectedItems" method, or "SelectedIndices", or if MultiSelectFieldSettings has a "checked" property, but no luck.
Any help would be appreciated.
Thanks!
SIGN IN To post a reply.
1 Reply
PO
Prince Oliver
Syncfusion Team
March 20, 2019 09:21 AM UTC
Hello Donald,
Good day to you.
Based on your requirement to preselect value in Multiselect from the server-side, we would suggest you go with the MuliselectFor control. You can directly pass the value through the model to the control from the server side. kindly refer the following code for that
[View]
|
@Html.EJS().MultiSelectFor(model => model.value).Placeholder("Select Categories").Mode(VisualMode.CheckBox).DataSource((IEnumerable<Category>)Model.categories).Fields(new MultiSelectFieldSettings { Text = "Name", Value = "Id" }).Render() |
[Controller]
|
public ActionResult Index()
{
DataModel model = new DataModel();
model.categories = new Category().getNameLists();
model.value = new string[] { "2", "3" };
return View(model);
} |
Also in your current shared code, you can achieve the requirement using ViewBag data and Value property in the control. Please refer the below code.
[View]
|
@Html.EJS().MultiSelect("products").Placeholder("Select Categories").Value(ViewBag.value).Mode(VisualMode.CheckBox).DataSource((IEnumerable<Category>)ViewBag.data).Fields(new MultiSelectFieldSettings { Text = "Name", Value = "Id" }).Render() |
[Controller]
|
public ActionResult Index()
{
ViewBag.data = new Category().getNameLists();
ViewBag.value = new string[] { "2", "3" };
return View();
} |
Please find an example that demonstrates the above in the following location: http://www.syncfusion.com/downloads/support/forum/143422/ze/MVCsampleEJ21683230195
Let us know if you need any further assistance on this.
Regards,
Prince
SIGN IN To post a reply.
- 1 Reply
- 2 Participants
-
DS Donald Snowdy
- Mar 20, 2019 12:11 AM UTC
- Mar 20, 2019 09:21 AM UTC