This project is read-only.

Editor Not Populating ViewModel Data

Feb 4, 2011 at 3:34 AM
Edited Feb 4, 2011 at 3: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?

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()
                                       new AccountEntry
                                           Account = s,
                                           IsChecked = itemAccounts.Contains(s.Id)


The view code


@model SmudgeIT.Feed.ViewModels.EditAccountsViewModel
            var accountIndex = 0;
        @foreach (var account in Model.Accounts)
                <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>} />



Feb 4, 2011 at 4:17 AM

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

Feb 4, 2011 at 12:12 PM

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);



Feb 4, 2011 at 6: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).