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

Binding a ListBox to the MVC Model

3 Answers 277 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Eric D. Burdo
Top achievements
Rank 2
Eric D. Burdo asked on 06 Nov 2018, 09:30 PM
I am using some code pulled from one of the Kendo examples for connecting two ListBoxes (populating the first with an AJAX call, and then letting the user transfer some of the items to the second listbox.

 

I currently have this set with an "EditorFor" approach.

Everything works fine, except that my Model never gets the selected users.

In the Model, that field is: 

IEnumerable<UserModel> SelectedEmployees

 

No matter what I've tried, the selected employees never get sent back to the Model with the other bits.

If I use jQuery to dump the dataitems to the Console, I can see what users have been selected.  They just don't go back to my model.

 

Is this possible?  Am I way out to lunch?  Or just missing something?

 

@using WMGPipeline.Models
@model IEnumerable<UserModel>
 
<div>
  <input type='text' id='searchBox' class='k-textbox' placeholder='Search By Name' />
  <br />
 
  @(Html.Kendo().ListBox()
        .Name("employees")
        .Toolbar(toolbar => {
          toolbar.Position(Kendo.Mvc.UI.Fluent.ListBoxToolbarPosition.Right);
          toolbar.Tools(tools => tools
            .TransferTo()
            .TransferFrom()
            .TransferAllTo()
            .TransferAllFrom()
            .Remove()
            );
        })
        .ConnectWith("SelectedEmployees")
        .DataTextField("LastFirst")
        .DataValueField("UserID")
        .DataSource(source => {
          source.Custom()
          .ServerFiltering(true)
          .Type("aspnetmvc-ajax")
          .Transport(transport => {
            transport.Read("getEmployees", "Pipeline");
          })
          .Schema(schema => {
            schema.Data("Data").Total("Total");
          });
        })
  )
  @(Html.Kendo().ListBox()
        .Name("SelectedEmployees")
        .DataTextField("LastFirst")
        .DataValueField("UserID")       
        .BindTo(Model)
        .Selectable(ListBoxSelectable.Multiple)
  )
/div>

3 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 08 Nov 2018, 12:38 PM
Hello Eric,

By default the ListBox does not provide two-way binding. If you would like to submit the items from one of the ListBox widgets I would suggest using the approach described in the following article:



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
Eric D. Burdo
Top achievements
Rank 2
answered on 08 Nov 2018, 12:45 PM

Thank you Viktor,

Looking at the sample you provided, I believe my solution would then be to move away from the BeginForm() syntax for submission, 

@using (Html.BeginForm("savePipeline", "Pipeline")) {

 

And switch to using an Ajax Post on the submit button... passing in my Model, and the extra data needed.

 

Is that correct?

0
Viktor Tachev
Telerik team
answered on 09 Nov 2018, 11:27 AM
Hi Eric,

Indeed when using the approach I suggested there will be no need to have a form on the page since the data will be submitted with an ajax request. 

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.
Tags
ListBox
Asked by
Eric D. Burdo
Top achievements
Rank 2
Answers by
Viktor Tachev
Telerik team
Eric D. Burdo
Top achievements
Rank 2
Share this question
or