Listbox selected items to Controller

Hi,

I'm using a multiple-selection listbox to enhance a list of GUIDs, but the controller only receives one value and not all of the selected ones in the listbox.

View:

            @Html.EJS().ListBoxFor(m => m.IdAziendeTrasportatrici, Model.IdAziendeTrasportatrici).CssClass("form-control-grid").SelectionSettings(new Syncfusion.EJ2.DropDowns.ListBoxSelectionSettings { Mode = Syncfusion.EJ2.DropDowns.SelectionMode.Multiple, ShowCheckbox = true }).DataSource((IEnumerable<object>)ViewBag.AziendeTrasportatrici).Fields(new Syncfusion.EJ2.DropDowns.ListBoxFieldSettings { Text = "RagioneSociale", Value = "IdAziendaTrasportatrice" }).Render()

ViemModel:

 public List<Guid> IdAziendeTrasportatrici { get; set; }


12 Replies

GK Gayathri KarunaiAnandam Syncfusion Team July 8, 2021 04:07 AM UTC

Hi Pio Luca Valvona, 

We have checked your reported query. We need to validate more on this query. So, we will update you the further details on July 8th, 2021. We appreciate your patience until then. 

Regards, 
Gayathri K 



PL Pio Luca Valvona July 12, 2021 04:51 PM UTC

Are there any news?



GK Gayathri KarunaiAnandam Syncfusion Team July 13, 2021 04:14 PM UTC

Hi Pio, 
  
Thanks for the patience. 

We have checked your reported query. We are unable to replicate the reported issue in our end. We have prepared a sample based on your suggestion. Please check the below code snippet. 

@using (Html.BeginForm()) 
{ 
    @Html.EJS().Button("element").Content("Button").Render() 
    <br /> 
    @Html.EJS().ListBoxFor(model => model.CountyId).CssClass("form-control-grid").SelectionSettings(new Syncfusion.EJ2.DropDowns.ListBoxSelectionSettings { Mode = Syncfusion.EJ2.DropDowns.SelectionMode.Multiple, ShowCheckbox = true }).DataSource((IEnumerable<object>)ViewBag.data).Fields(new Syncfusion.EJ2.DropDowns.ListBoxFieldSettings { Text = "Name", Value = "Name" }).Render() 
} 


public class Countries 
    { 
        public string Name { get; set; } 
        public string Code { get; set; } 
        public string[] CountyId { get; set; } 
        public List<Countries> CountriesList() 
        { 
            List<Countries> country = new List<Countries>(); 
            country.Add(new Countries { Name = "Australia", Code = "AU" }); 
            country.Add(new Countries { Name = "Bermuda", Code = "BM" }); 
            country.Add(new Countries { Name = "Canada", Code = "CA" }); 
            country.Add(new Countries { Name = "Cameroon", Code = "CM" }); 
            country.Add(new Countries { Name = "Denmark", Code = "DK" }); 
            country.Add(new Countries { Name = "France", Code = "FR" }); 
 
            return country; 
        } 
    } 
    public class HomeController : Controller 
    { 
 
        public ActionResult Index() 
        { 
            Countries model = new Countries(); 
            ViewBag.data = new Countries().CountriesList(); 
            model.CountyId = new string[] { "Cameroon" , "Denmark"}; 
            return View(model); 
 
        } 
        [HttpPost] 
        public ActionResult Index(Countries model) 
        { 
 
            ViewBag.data = new Countries().CountriesList(); 
            return View(model); 
        } 
 


For your reference, we have prepared a sample based on your requirement. Please check the below sample. 


If you are still facing the issue, kindly share the below details. 
  • If possible, try to reproduce the reported issue in provided sample or share the issue reproducible sample.
  • If possible, please share the video demonstration of the issue.
  • Please share the Syncfusion version you are using.

Please provide the above requested information, based on that we will check and provide you a better solution quickly. 

Regards, 
Gayathri K 



PL Pio Luca Valvona July 13, 2021 06:43 PM UTC

Hi Gayathri,

thank you for support.

The proposed solution also does not work.

I'll attach a sample solution to show you the error.


Attachment: WebApplication8390716061_999887bd.zip


GK Gayathri KarunaiAnandam Syncfusion Team July 15, 2021 04:04 AM UTC

Hi Pio Luca Valvona,  

We have checked your reported query and sample. We need to check more on this. So, we will update you the further details on or before July 16th, 2021. We appreciate your patience until then.  

Regards,  
Gayathri K  



AS Aravinthan Seetharaman Syncfusion Team July 22, 2021 03:35 AM UTC

 
Thanks for the patience. 
 
We have checked your query.For performing CRUD action in the Grid, a primary key column is required. To define the primary key, set isPrimaryKey property of Column to true in particular column.  
  
More details on this can be checked in the below documentation link,  
  
  
And when remote save adaptor is used, the CRUD actions will be performed in the server side, so we need to specify the corresponding action URL’s in the following methods of the Grid’s data Source – “InsertUrl”, “RemoveUrl” and “UpdateUrl”. So for this case, the insert Url needs to be defined with the controller action method as demonstrated in the below code snippet. 
  
@(  
Html.EJS().Grid<Test>("GridProdotti").ActionBegin("onActionBegin").DataSource(dataManager => { dataManager.Json(((IEnumerable<GridData>)ViewBag.dataSource).ToArray()).InsertUrl("/Home/Insert").Adaptor("RemoteSaveAdaptor"); }).Render()  
)  
  
public ActionResult Insert(CRUDModel1<GridData> dm)  
{  
    // Here the inserted data needs to be updated in the back end data and returned back to the Grid  
    new GridData().DataList().Insert(0, dm.value);  
    return Json(dm.value);  
}  
  
More details on this can be checked in the below documentation link,  
  
  
Also the EJ2 Grid does not have support for array column type. The supported column types are mentioned below,  
  
  • string
  • number
  • boolean
  • date
  • datetime
  
So for saving the list selected text in the Grid, in the actionBegin event handler we need to retrieve the text values from the edit list-box, convert it to a single string value and assign it to its corresponding data field returned in the event arguments as demonstrated in the below code snippet,  
  
// Grid’s actionBegin event handler  
function onActionBegin(args) {  
    if (args.requestType === "save") {  
        var listInstance = args.form.querySelector("#CountyId").ej2_instances[0];  
        var itemValues = "";  
        listInstance.getSelectedItems().forEach(ele => {  
            if (itemValues === "")  
                itemValues = ele.innerText;  
            else  
                itemValues = itemValues + ", " + ele.innerText;  
        });  
        // The list control selected text is retrieved as single string and set to its corresponding data field  
        args.data["CountyId"] = itemValues;  
    }  
}  
  
For your convenience we have prepared sample here. 
  
  
Please let me know if you have any concerns.  
 
Regards, 
Aravinthan S


PL Pio Luca Valvona July 27, 2021 01:25 PM UTC

Hi  Aravinthan,

Thank you for support.

Perfect! 

Nice solution.



AS Aravinthan Seetharaman Syncfusion Team July 28, 2021 05:31 AM UTC

Hi Pio Luca Valvona, 
 
Thanks for the update. 
 
We are happy to hear that your issue has been resolved. Please feel free to contact us if you need any further assistance on this. 
 
Regards, 
Aravinthan S 



PL Pio Luca Valvona July 28, 2021 06:21 PM UTC

Hi Aravinthan,

Unfortunately the proposed solution does not meet the requirements, I can not use a crudmodel1 class, I am obliged to use the model I created, as in example attached is Test class.

For this I need to receive an array of guid, also because I do not know how to select the values of the listbox based on those returned from the database.



AS Aravinthan Seetharaman Syncfusion Team July 29, 2021 03:39 PM UTC

  
We have checked your query. You can use your pre-defined model class(‘Test’) itself to retrieve multiple selected GUID values from the ListBox(rendered in Grid edit dialog) by using the below steps,  
  
Initially, as mentioned in our previous update please ensure to define a primary key column for the Grid(in order to perform CRUD actions on it) and specify the CRUD action url. Then define the input control for the corresponding primary key column also in the partial view(to be rendered in the dialog edit template).  
  
Now in the Grid’s actionBegin event, set the list box’s value property(instead of text as previously suggested) to its corresponding field as demonstrated in the below code snippet,  
  
function onActionBegin(args) {  
        if (args.requestType === "save") {  
             var listInstance = args.form.querySelector("#CountyId").ej2_instances[0];  
             args.data["CountyId"] = listInstance.value;  
         }  
}  
 
  
Now in the controller action method(set for inserting records in the Grid’s data manager instance), you can access the newly added value by extending your pre-defined model class(‘Test’) to the CRUD model class.  
  
public ActionResult Insert(CRUDModel1<Test> dm)  
{  
    // Here you can access the GUID values from ‘dm.value’  
    List<Test> model = new List<Test>();  
    model.Add(dm.value);  
    return Json(dm.value);  
}  
  
public class CRUDModel1<T>: Test  
{  
    public string action { getset; }  
    public string keyColumn { getset; }  
    public int key { getset; }  
    public T value { getset; }  
}  
  
public class Test  
{  
    public Guid[] CountyId { getset; }  
    public string t { getset; }  
    public int Id { getset; }  
}  
 
  
On doing this you will be able to receive the selected GUID values of the list box as shown in the below image,  
  
 
 
 
 
 
Please find the modified sample for your reference below,  
  
  
Could you please check the above details and get back to us, if you need assistance on this. 
 
Regards, 
Aravinthan S 



PL Pio Luca Valvona July 29, 2021 05:30 PM UTC

Hi  Aravinthan,

Thank you for support.

Perfect! 

Now proposed solution meet the requirements .

Nice job.



AS Aravinthan Seetharaman Syncfusion Team July 30, 2021 05:49 AM UTC

Hi Pio, 
 
Thanks for the update. 
 
We are happy to hear that your issue has been resolved. Please feel free to contact us if you need any further assistance on this. 
 
Regards, 
Aravinthan S 


Loader.
Up arrow icon