This is a migrated thread and some comments may be shown as answers.

send selected values to controller

10 Answers 1153 Views
MultiSelect
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 2
Thomas asked on 14 Oct 2013, 10:10 AM
Hi Kendo community,

I don't really understand how the selected objects out of a multiselect can be sent to the controller. The databinding out of the db works fine and I'm also able to select and remove items in the multiselect. By the way the multiselect is displayed inside a kendo window and posts with an ajax.beginform.

My multiselect looks like this:

@model SoftwareAdminInterface.Models.Administration.Pattern
@
using (Ajax.BeginForm("SetCombi", "Pattern", new { }, new AjaxOptions() { HttpMethod = "post", UpdateTargetId = "myContentPopupEditRole_div" }))
  
@(Html.Kendo().MultiSelect()
                    .MaxSelectedItems(2)
                    .Name("RegExID")
                    .DataTextField("RegExName")
                    .DataValueField("RegExID")
                    .Placeholder("Select Patterns...")
                    .AutoBind(false)    
                    .DataSource(source => {
                        source.Read(read =>
                        {
                            read.Action("GetPatternsForCombi", "Pattern");
                        })
                    .ServerFiltering(true);
          })
    )

What I want to do now is to pass the two selected RegExID's over to the controller. Controller action looks like this:
[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SetCombi([DataSourceRequest] DataSourceRequest request, List<Pattern> selectedPatterns)
        {
            SoftwareHelper helper = new SoftwareHelper();
 
            return PartialView("Combination");
        }

basically I thought of getting the IDs from the List<Pattern>, but the list is null.
any advise?

thx in advance
kind regards

10 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 16 Oct 2013, 10:51 AM
Hello,

The selected values will be posted when using a form and not the objects. Thus, you should use a collection of the value field type as parameter of the action. The action parameter name should also be the same as the multiselect name:

public ActionResult SetCombi(List<RegExIDType> RegExID)
Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Cool Breeze
Top achievements
Rank 1
answered on 25 Mar 2014, 05:41 AM
Hey Daniel, I've got pretty much the same issue. I've got the control working and the form posting back to the controller but my list of objects always has a count of 0. In the above example, I don't quite understand what RegExIDType is. You say "use a collection of the value field type as parameter of the action". Well, the control is getting populated by "GetPatternsForCombi" which I'm assuming returns a List<Pattern> where RegExID is a property of Pattern as well as RegExName. So, how is RegExIDType different than Pattern?

Thanks!
0
Cool Breeze
Top achievements
Rank 1
answered on 25 Mar 2014, 05:46 AM
Haha nevermind, I figured out what you mean (Guid, int etc). The actual data type of the field that the DataValueField is set to. Thanks!
0
bUb
Top achievements
Rank 1
answered on 18 Jan 2018, 06:40 PM

How can you get the selected values from within a web API controller and not use model binding?  Like in the following controller's method signature for a create;

public async Task<IHttpActionResult> PostFormData()

0
Nencho
Telerik team
answered on 22 Jan 2018, 01:00 PM
Hello Monty,

Could you please elaborate a bit more on your scenario. Since this thread is regarding the MultiSelect, you could you specify if you are trying to pass data to the controller trough the widget or this is a general questions. If it is, I would suggest to revise the following article:

http://techbrij.com/pass-parameters-aspdotnet-webapi-jquery

Regards,
Nencho
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
bUb
Top achievements
Rank 1
answered on 22 Jan 2018, 02:54 PM

If I make a kendomultiselect object in the form and have form's target be an ASP.NET API Controller and the action method's signature does not use model binding.  How do I get the data from the form?  I have inspected the HTTP Request in a bunch of debug scenarios and it seems as if the multiselect's attribute name comes across the wire a little different than a normal option input.  I expected to see one http attribute name with an array of string values but instead saw the attribute name come across the request multiple times with different values corresponding to what was selected.

 

Is there a way to define both a remote read datasource for the multiselect's options and a way to define the selected values from that datasource?

0
Nencho
Telerik team
answered on 24 Jan 2018, 01:38 PM
Hello,

Thank you for the clarification.

Having a clearer picture on the scenario that you aim to achieve, I would advise you to refer to the following documentation article, describing a technique for traversing the needed form data to the specified method. Actually, this scenario is not direly related with our component, but to a general approach or mechanism that needs to be used in this case:

https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-1

Regards,
Nencho
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
bUb
Top achievements
Rank 1
answered on 24 Jan 2018, 03:00 PM

That URL you referenced uses model binding.  I specifically was asking without model binding.  I know that a kendomultiselect's request can be bound to a string array.  Not using model binding it seems as if the multiselect sends multiple values separately instead of in an array, using Request.Content's read methods.

The second question is about defining the options and the selected options in the multiselect via remote datasources.

0
Nencho
Telerik team
answered on 26 Jan 2018, 09:48 AM
Hello,

I am afraid that the Kendo UI Multselect is actually a select element, this is why, the transmit of the data to the webApi controller is key/value, instead of one array. As for your second question - the widget is not configured to consider a possible selection from the remote data source. In other words, you would need to traverse the dataitems for from the datasource, find the needed fields (selected probably) and use the .Value method from the Client API of the widget, in order to set those items as selected.

Regards,
Nencho
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
jay taylor
Top achievements
Rank 1
answered on 08 Feb 2018, 03:47 PM

Something like this worked for me. You could add something like  .Data("getFilter"); after your Action statement:

     

            source.Read(read =>
                        {
                            read.Action("GetPatternsForCombi", "Pattern")

                           .Data("getMyFilter");;

                        })

        function getMyFilter() {
            var filterValue = $("#myMultiselect").data("kendoMultiSelect").input.val();
            return { paramName: filterValue };
        }

This gives you a comma-separated string of values you'd parse at the controller.

 

Tags
MultiSelect
Asked by
Thomas
Top achievements
Rank 2
Answers by
Daniel
Telerik team
Cool Breeze
Top achievements
Rank 1
bUb
Top achievements
Rank 1
Nencho
Telerik team
jay taylor
Top achievements
Rank 1
Share this question
or