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

MultiSelect modelbinding in a loop and form post

3 Answers 37 Views
MultiSelect
This is a migrated thread and some comments may be shown as answers.
erwin
Top achievements
Rank 1
Veteran
erwin asked on 27 Aug 2017, 11:49 AM

Hi,

I have a list of objects in my model

   public class SoftwareItemPropertiesViewModel
    {
....
        public List<Annotation> Annotations { get; set; } = new List<Annotation>();
....

 

that I render to multiple multiselect like so:

for (var i = 0; i < Model.Annotations.Count; i++)
      {
 
          <div class="form-group">               
              @Html.Label(Model.Annotations[i].Name)
              <div class="col-sm-10">
                  @(Html.Kendo().MultiSelectFor(m => m.Annotations[i].SelectedOptions)
                        .DataTextField("Name")
                        .DataValueField("Value")
                        .BindTo(Model.Annotations[i].Options)
                        .Placeholder(Model.Annotations[i].Placeholder)
                        )                   
              </div>
          </div>
 
      }

 

On post of the form, I can see that the selected data is there as as expected in Annotations[0].SelectedOptions:

Annotations%5B0%5D.SelectedOptions: 4
Annotations%5B0%5D.SelectedOptions: 5
Cost: 515
Description: adfasdf
Guid: 40f339ed-b1e8-4a46-a987-8d5d79ec0be0
Name: abcasdfsadf
OwnerNotes:
SelectedOwners: 1
SelectedOwners: 2
Version:

 

However, the selected Values are not populated back in the Model object that I receive.

Annotations List is populated with 1 Annotation as expected, but Annotations[0].SelectedOptions is not populated.

There is another Multiselect directly bound to the model and here I see Selected values in the model as expected.

What am I missing?

Regards Erwin

 

3 Answers, 1 is accepted

Sort by
0
erwin
Top achievements
Rank 1
Veteran
answered on 27 Aug 2017, 06:28 PM

Never mind. Missed the obvious.

Annotations[]SelectedOptions needs to be a public Property. A Public field does not work but also raises no errors with the compiler nor the model binder.

Regards

Erwin

0
Dimitar
Telerik team
answered on 29 Aug 2017, 11:06 AM
Hello Erwin,

I have reviewed the provided configuration and it looks valid. You are also correct that selectedOption need to be a public property in order for the binding to work.

Can you confirm if you managed to successfully resolve the issue with this modification?

Regards,
Dimitar
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
erwin
Top achievements
Rank 1
Veteran
answered on 29 Aug 2017, 11:13 AM

Hi Dimitar,

sorry if I was unclear in my last message. Changing from public Field to public Property enabled the Binding and everything works as expected now.

What I find a bit confusing is that referencing a public field works in the View but cannot be populated by the standard model binder. But that is standard ASP.NET behaviour, nothing specific to Telerik.

Regards

 

Erwin

 

 

Tags
MultiSelect
Asked by
erwin
Top achievements
Rank 1
Veteran
Answers by
erwin
Top achievements
Rank 1
Veteran
Dimitar
Telerik team
Share this question
or