how get multiselect's value

Hello,

I don't find how to get multiselect's value in my controller. I need it to insert or update Database.

Best Regards
Thank you

5 Replies

KR Keerthana Rajendran Syncfusion Team June 13, 2018 05:43 AM UTC

Hi TheDarkBoz,    
   
Thank you for contacting Syncfusion Support.   
   
You can get the selected values during form post as list of string or as array of strings through MultiSelect id. Please refer to the below given code.   
   
View:   
   
@using (Html.BeginForm("Index""Home", FormMethod.Post))   
{   
    <div>   
        <ejs-multiselect id="games" dataSource="@ViewBag.data" placeholder="Favorite Sports" mode="Box">   
            <e-multiselect-fields value="Id" text="Game"></e-multiselect-fields>   
        </ejs-multiselect>   
        <button id="reload" type="submit">Submit Values</button>   
    </div>   
}   
   
Controller:   
   
public IActionResult Index()   
        {   
            ViewBag.data = new GameList().GameLists();   
            return View();   
        }   
        [HttpPost]   
        public IActionResult Index(List<string> games)   
        {   
                        
         ViewBag.data = new GameList().GameLists();   
          var selectedValues = games;   
            return View();   
        }   
    public class GameList   
    {   
        public string Id { getset; }   
        public string Game { getset; }   
   
        public List<GameList> GameLists()   
        {   
            List<GameList> game = new List<GameList>();   
            game.Add(new GameList { Id = "Game1", Game = "Football" });   
            game.Add(new GameList { Id = "Game2", Game = "Badminton" });   
            game.Add(new GameList { Id = "Game3", Game = "Basketball" });   
            game.Add(new GameList { Id = "Game4", Game = "Cricket" });   
            game.Add(new GameList { Id = "Game5", Game = "Football" });   
            game.Add(new GameList { Id = "Game6", Game = "Golf" });   
            game.Add(new GameList { Id = "Game7", Game = "Hockey" });   
            game.Add(new GameList { Id = "Game8", Game = "Rugby" });   
            game.Add(new GameList { Id = "Game9", Game = "Snooker" });   
            game.Add(new GameList { Id = "Game10", Game = "Tennis" });   
            return game;   
        }   
    }   
   
   
(OR)   
   
[HttpPost]   
        public IActionResult Index(string[] games)   
        {   
            ViewBag.data = new GameList().GameLists();   
           var selectedValues = games;   
            return View();   
        }   
   
   
Now you can get the selected values as shown below. From here you can update this to your database.   
   
    
   
   
   
We have attached a sample for your reference which can be downloaded from the below link   
   
   
Regards,   
Keerthana.   
  
 



FS Farhan Syed August 30, 2018 10:56 PM UTC

we can only use viewbag and cannot use viewmodel this is huge limitation, please provide me solution with viewmodel  otherwise it is really frustrating i spent half of my day in this :(

Thanks
Farhan


KV Karthikeyan Viswanathan Syncfusion Team August 31, 2018 09:17 AM UTC

Hi Farhan, 
 
Based on your scenario, you can set and get the model value by using ejs-for attribute.  
 
Please refer the below code example: 
 

[View] 
@model multiviewmodel.Controllers.Games 
 
<form asp-action="Index" asp-controller="Home" method="post"> 
    <div> 
        <ejs-multiselect ejs-for="@Model.Game" name="Game" id="Game" dataSource="@Model.Data" placeholder="Favorite Sports" mode="Box"> 
            <e-multiselect-fields value="Id" text="Game"></e-multiselect-fields> 
        </ejs-multiselect> 
        <button id="reload" type="submit">Submit Values</button> 
    </div> 
</form> 

[Controller] 
public IActionResult Index() 
{ 
    Games game = new Games(); 
    game.Data = new GameList().GameLists(); 
    game.Game = new string[] { "Game7" }; 
    return View(game); 
} 
 
[HttpPost] 
public IActionResult Index(Games games) 
{ 
    games.Data = new GameList().GameLists(); 
    return View(games); 
} 

[Model] 
public class Games 
{ 
    public string[] Game { get; set; } 
    public List<GameList> Data { get; set; } 
} 


Regards, 
Karthikeyan V. 



FS Farhan Syed August 31, 2018 06:18 PM UTC

it is not working for me i tested that, maybe this is in new version of Grid.

Thanks
Farhan


KV Karthikeyan Viswanathan Syncfusion Team September 3, 2018 08:56 AM UTC

Hi Farhan,   
  
Sorry for the inconvenienced.  
  
We were unable to find out the exact root cause of this issue. So, please share the below details. It will help us to provide a solution at earlier.  
  • Share the issue reproducing sample.
  • Share the shared sample as issue reproducible one.
  • Share the code snippet which will help to reproduce this issue.
  • Share the package and version details has used in your application.
  
Regards,  
Karthikeyan V. 


Loader.
Up arrow icon