We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Selected items in MultiSelect

Thread ID:

Created:

Updated:

Platform:

Replies:

143422 Mar 20,2019 12:11 AM UTC Mar 20,2019 09:21 AM UTC ASP.NET MVC - EJ 2 1
loading
Tags: MultiSelectDropdown
Donald Snowdy
Asked On March 20, 2019 12:11 AM UTC

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".

@Html.EJS().MultiSelect("products").Placeholder("Select Categories").Mode(VisualMode.CheckBox).DataSource((IEnumerable<Category>)Model).Fields(new MultiSelectFieldSettings { Text = "Name", Value = "Id" }).Render()


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!

Prince Oliver [Syncfusion]
Replied On 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  


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;