0

I have researched all over the net but hopefully here someone can help me.

I have following ViewModel classes:

public class PersonEditViewModel
{
    public Person Person { get; set; }
    public List<DictionaryRootViewModel> Interests { get; set; }
}

public class DictionaryRootViewModel
{
    public long Id { get; set; }
    public string Name { get; set; }
    public ICollection<DictionaryItemViewModel> Items;

    public DictionaryRootViewModel()
    {
        Items = new List<DictionaryItemViewModel>();
    }
}

public class DictionaryItemViewModel
{
    public long Id { get; set; }
    public string Name { get; set; }
    public bool Selected { get; set; }
}

In the Edit view I use custom EditorTemplate to layout collection of Interests using @Html.EditorFor(m => m.Interests). There are two EditorTemplates that do the rendering:

  1. DictionaryRootViewModel.cshtml:

    @model Platforma.Models.DictionaryRootViewModel
    @Html.HiddenFor(model => model.Id)
    @Html.HiddenFor(model => model.Name)
    @Html.EditorFor(model => model.Items)
    
  2. DictionaryItemViewModel.cshtml:

    @model Platforma.Models.DictionaryItemViewModel
    @Html.HiddenFor(model => model.Id)
    @Html.CheckBoxFor(model => model.Selected)
    @Html.EditorFor(model => model.Name)
    

The problem:

When submitting the form using POST, only the Interests collection gets populated and the Interest.Items collection is always empty. The request contains (among others) following field names, they are also present when inspecting Request.Forms data in the controller action method.

  • Interests[0].Id
  • Interests[0].Name
  • Interests[0].Items[0].Id
  • Interests[0].Items[0].Selected

All contain proper values - but on the controller side, the object pvm in method:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(PersonEditViewModel pvm)
{
}

contains data in Interests collection (items with correct id and name), but for each element of the collection its' subcollection of Items is empty.

How do I make it to correctly pick the model?

Rax
  • 29
  • 4

2 Answers2

2

As usual - the answer was in front of me the whole time. The DefaultModelBinder didn't pick up the Items values passed in request, because I "forgot" to mark Items collection as property - it was a field! The correct form taking into account helpful remark by @pjobs:

public List<DictionaryItemViewModel> Items{get;set;}

Rax
  • 29
  • 4
0

Most of the time issue is with index, when you have collections posting, you either needs to have sequential index or have Interests[i].Items.Index hidden field if indexes are not sequential.

Here is similar question on SO

It will not work if you have

Interests[0].Id
Interests[0].Name
Interests[0].Items[0].Id
Interests[0].Items[0].Selected
Interests[0].Items[2].Id
Interests[0].Items[2].Selected

So to fix it you either make sure have sequencial indexes as

Interests[0].Id
Interests[0].Name
Interests[0].Items[0].Id
Interests[0].Items[0].Selected
Interests[0].Items[1].Id
Interests[0].Items[1].Selected

OR

Interests[0].Id
Interests[0].Name
Interests[0].Items.Index = 0 (hidden field)
Interests[0].Items[0].Id
Interests[0].Items[0].Selected
Interests[0].Items.Index = 2 (hidden field)
Interests[0].Items[2].Id
Interests[0].Items[2].Selected
Community
  • 1
  • 1
pjobs
  • 1,247
  • 12
  • 14
  • Hi, Thank you for the help! Unfortunately it does not seem to work. The first approach was already implemented, since I'm using built-in templating features of ASP.NET MVC5. I tried the other one and the results were the same. – Rax Apr 22 '15 at 14:36
  • can we see more info from your postback , I mean all things of your Request.Forms data – pjobs Apr 22 '15 at 14:41
  • Another thing is can you change public ICollection Items to public List Items in your ViewModel and try – pjobs Apr 22 '15 at 14:43
  • Hi, the change to List from ICollection doesn't help. All the fields from the request are here: http://pastebin.com/RgaKQEre . You can see a few more collections of collections - all of them fail in the same way as Interests collection. – Rax Apr 22 '15 at 14:52
  • wow thats lots of collection of collections, you got these from Posted Form...? very big form – pjobs Apr 22 '15 at 15:23