Editor Not Populating ViewModel Data

Feb 4, 2011 at 2:34 AM
Edited Feb 4, 2011 at 2:35 AM

I have used the n-n tutorial and I am having trouble getting the data back from the view to the post editor method in my driver.

In the return editor method 

updater.TryUpdateModel<EditAccountsViewModel>(model, Prefix, new string[] { "Accounts" }, null);
always returns null what am I doing wrong?
Thanks 

The driver code

 

  protected override DriverResult Editor(AccountsPart part, dynamic shapeHelper)
        {

            return ContentShape("Parts_Accounts_Edit",
                () => shapeHelper.EditorTemplate(
                         TemplateName: TemplateName,
                         Model: BuildEditorViewModel(part))
                         );
        }

        protected override DriverResult Editor(
             AccountsPart part,
             IUpdateModel updater,
             dynamic shapeHelper)
        {
            var model = new EditAccountsViewModel();
            var res = updater.TryUpdateModel<EditAccountsViewModel>(model, Prefix, new string[] { "Accounts" }, null);

            if (part.ContentItem.Id != 0)
            {
                _accountService.UpdateAccountsForContentItem(part.ContentItem, model.Accounts);
            }

            return Editor(part, shapeHelper);
        }


        private EditAccountsViewModel BuildEditorViewModel(AccountsPart part)
        {
            var itemAccounts = part.Accounts.ToLookup(r => r.Id);
            return new EditAccountsViewModel
            {
                Accounts = (from s in _accountService.GetAccounts()
                                   select
                                       new AccountEntry
                                       {
                                           Account = s,
                                           IsChecked = itemAccounts.Contains(s.Id)
                                       }
                    ).ToList()
            };
        }

 

The view code

 

@model SmudgeIT.Feed.ViewModels.EditAccountsViewModel
<fieldset>
    <legend>@T("Accounts")</legend>
    <ul>
        @{
            var accountIndex = 0;
        }
        @foreach (var account in Model.Accounts)
        {
            <li>
                <input type="hidden" value="@account.Account.Id" name="@Html.FieldNameFor(m => m.Accounts[accountIndex].Account.Id)"/>
                <label for="@Html.FieldNameFor(m => m.Accounts[accountIndex].IsChecked)">

                <input type="checkbox" value="true" name="@Html.FieldNameFor(m => m.Accounts[accountIndex].IsChecked)" id="@Html.FieldNameFor(m => m.Accounts[accountIndex].IsChecked)"
                @if (account.IsChecked)
                {<text>checked="checked"</text>} />

                @string.Format("@{0}",@account.Account.Username)
                </label>
                @{accountIndex++;}
            </li>
        }
    </ul>
</fieldset>

 

Coordinator
Feb 4, 2011 at 3:17 AM

Why is the type of model different from the type parameter?

Feb 4, 2011 at 11:12 AM

I updated it to this and still no luck.  Am I right in thinking that TryUpdateModel maps the data from the form to the model in this case EditAccountsViewModel?

 

var model = new EditAccountsViewModel();          
var res = updater.TryUpdateModel(model, Prefix, new string[] { "TwitterAccounts" }, null);

 

 

Coordinator
Feb 4, 2011 at 5:59 PM

It's attempting to, and it works as long as the form was built according to the conventions that the model binder expects. There is probably a subtle thing that's off in the way those form fields are being built. Maybe looking at the exact form payload that was submitted with a traffic analyzer (Firebug or Fiddler should work) might help you figure it out. You might also want to debug deeply into that call to TryUpdateModel into the binder and see where it fails.

(in other words nothing is jumping at me as shockingly wrong).