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
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)
Daniel
Telerik
Thanks!
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()
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
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?
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
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.
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
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.