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

passing data from view to controller

8 Answers 1423 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Julien
Top achievements
Rank 1
Julien asked on 25 Apr 2018, 05:06 AM

Hello,

I would like to know how to pass the data from my "SelectedPosteSelectList" to my controller. When I submit my form, my "SelectedPosteSelectList" is null.

 

Thank you

 

public class EquipeViewModel
    {
        public string Nom { get; set; }
 
        public string Description { get; set; }
 
        public List<SelectListItem> PostesSelectList { get; set; }
 
        public List<SelectListItem> SelectedPosteSelectList { get; set; }
 
    }

 

<form asp-action="Edit">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Nom" class="control-label"></label>
                <input asp-for="Nom" class="form-control" />
                <span asp-validation-for="Nom" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Description" class="control-label"></label>
                <input asp-for="Description" class="form-control" />
                <span asp-validation-for="Description" class="text-danger"></span>
            </div>
            <input type="hidden" asp-for="Id" />

       

            <div id="example" role="application">
                <div class="demo-section k-content wide">
                    <label for="optional" id="employees">Postes</label>
                    <label for="selected">@Model.Nom</label>
                    <br />
                    @(Html.Kendo().ListBox()
                                        .Name("Postes")
                                        .Toolbar(toolbar =>
                                        {
                                            toolbar.Position(ListBoxToolbarPosition.Right);
                                            toolbar.Tools(tools => tools
                                                .TransferTo()
                                                .TransferFrom()
                                                .TransferAllTo()
                                                .TransferAllFrom()
                                            );
                                        })
                                        .Selectable(ListBoxSelectable.Multiple)
                                        .ConnectWith("SelectedPosteSelectList")
                                        .BindTo(Model.PostesSelectList)
                    )
 
                    @(Html.Kendo().ListBox()
                                        .Name("SelectedPosteSelectList")
                                        .BindTo(@Model.SelectedPosteSelectList)
                                        .Selectable(ListBoxSelectable.Multiple)
                    )
                </div>
            </div>
 
            <div class="form-group">
                <input type="submit" value="Sauvegarder" class="btn btn-success" />
                <a class="btn btn-filter" asp-action="Index">Retour</a>
            </div>
        </form>
 
public async Task<IActionResult> Edit(int id, EquipeViewModel equipeView)

8 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 27 Apr 2018, 11:05 AM
Hi Julien,

Please check out the thread below that discusses how the data in the ListBox cna be passed to the server. 


Let me know how the suggested approach works on your end. 

Regards,
Viktor Tachev
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
Jason
Top achievements
Rank 1
answered on 22 Oct 2018, 04:32 PM

Viktor;

I followed the thread, however within .net Core this does not seem to work.

Could you post a .net Core example?

0
Viktor Tachev
Telerik team
answered on 25 Oct 2018, 11:09 AM
Hi,

The approach when using .NET Core would be pretty much the same. The difference would be how the data is passed to the server. Check out the highlighted code below for reference.


$('#submit-form').on('click', function () {
    $.ajax({
        type: "POST",
        url: "/Home/Create",
        data: { selectedItems: $('#selected').data('kendoListBox').dataItems().toJSON() },
        dataType: "json",
        success: function (result) {
            alert("Successfully sent to server: " + result.map(function (x) {
                return x.Text
            }))
        }
    });
});


I am also attaching a sample project that illustrates the approach.


Regards,
Viktor Tachev
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
khairunnisa
Top achievements
Rank 1
answered on 31 Oct 2018, 09:23 AM

Hi Viktor Tachev,

Your solution seems work for that case but I got different kind of issue which is I'm using Data Source to bind the data. How can I pass the value to the controller?

Thank you.

0
Viktor Tachev
Telerik team
answered on 02 Nov 2018, 02:39 PM
Hi Khairunnisa,

The approach will work regardless of the way ListBox gets its items. The dataItems() method will return the items available in the widget. In case you would like to get only the selected items you can use the select() method.

Regards,
Viktor Tachev
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Stephen
Top achievements
Rank 1
answered on 27 Jan 2020, 10:45 PM

Hi Viktor,

Does it possible to pass the data both selected list to a ASP.NET Core Razor Handler, just like a grid, without using javascript AJAX?

The screen shot is what I'm trying to achieve.

Kindly see attachment.

0
Stephen
Top achievements
Rank 1
answered on 27 Jan 2020, 10:47 PM
Because what your trying to proposed a solution is for me to create a separate API endpoint just to persist the data.
0
Alex Hajigeorgieva
Telerik team
answered on 30 Jan 2020, 04:13 PM

Hello, Stephen,

You can use the Data() method to send the items to the controller if the ListBox is in a grid as editor. We have a project that demonstrates this exact way of handling the ListBox dataItems:

https://github.com/telerik/ui-for-aspnet-core-examples/blob/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Views/Grid/ListBoxAsEditor.cshtml#L43-L55

.Update(update => update.Action("Orders_Update", "ListBoxAsEditor").Data("sendAvailableLocations"))

 function sendAvailableLocations() {
        var items = $("#Available").data("kendoListBox").dataItems();
        var locations = items.map(function (item) { return item.toJSON()});
        return {
            availableLocations: locations
        }
    }

Regards,
Alex Hajigeorgieva
Progress Telerik

Get quickly onboarded and successful with Telerik UI for ASP.NET Core with the dedicated Virtual Classroom technical training, available to all active customers.
Tags
ListBox
Asked by
Julien
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Jason
Top achievements
Rank 1
khairunnisa
Top achievements
Rank 1
Stephen
Top achievements
Rank 1
Alex Hajigeorgieva
Telerik team
Share this question
or