Driver Editor (Post) not receiving form data

Topics: General, Writing modules
Apr 12 at 11:15 PM
I created a new part and attached it to the User Type. This part basically provides a dropdown on the User edit form of all administrators to help be more specific with email moderation notifications. Anyway, I have the dropdown working on the user edit form, but no matter what I do, the selected administrator will not post to the Driver. Any ideas?

Thanks,
Matt
Developer
Apr 13 at 10:25 AM
It sounds like a modelbinding issue. Did you check that the name attribute of the <select> element matches the property of the view model used when you perform the UpdateModel in your driver's UpdateEditor method?
Apr 13 at 4:29 PM
Edited Apr 13 at 4:32 PM
Yes, it seems to match in the HTML output.

View:
@model Dor.NotifyPublisher.ViewModels.UserPublisherViewModel

    @Html.LabelFor(m => m.PublisherName, T("Publisher"))
    @Html.DropDownListFor(m => m.PublisherName,
                                Model.Publishers.Select(p => new SelectListItem
                                {
                                    Selected = p.UserName == Model.PublisherName,
                                    Text = p.UserName,
                                    Value = p.UserName
                                }),
                                "Choose a Publisher..")
    @Html.ValidationMessageFor(m => m.PublisherName, "*")
ViewModel:
namespace Dor.NotifyPublisher.ViewModels
{
    [OrchardFeature("Dor.NotifyPublisher.UserPublisher")]
    public class UserPublisherViewModel
    {
        public string PublisherName { get; set; }
        public IEnumerable<UserPart> Publishers { get; set; }
    }
}
Driver Editor:

        protected override DriverResult Editor(UserPublisherPart part, Orchard.ContentManagement.IUpdateModel updater, dynamic shapeHelper)
        {
            var viewModel = new UserPublisherViewModel();
            if (updater.TryUpdateModel(viewModel, Prefix, null, null))
            {
                _userPublisherService.UpdateFormForContentItem(part.ContentItem, viewModel);
            }
            return Editor(part, shapeHelper);

        }
Developer
Apr 13 at 4:47 PM
Edited Apr 13 at 4:48 PM
Weird, it looks good. So the viewModel.PublisherName remains null right after the call to updater.TryUpdateModel? What do you see if you set a breakpoint in your Editor method, then walk up the call stack until you reach the controller code and inspect Request.Forms (via the Immediate window or watch window). What do the key and value look like in the Forms collection?
Apr 13 at 5:09 PM
AllKeys[17] "PublisherName" string

I can see the value in Form.Value: PublisherName=matt
Developer
Apr 13 at 5:19 PM
Are you also setting the Prefix property on the EditorTemplate shape in the overloaded version of the Editor method of the driver?
Marked as answer by matthewjamesr on 4/13/2016 at 11:41 AM
Apr 13 at 5:54 PM
No, I am not.
  [OrchardFeature("Dor.NotifyPublisher.UserPublisher")]
    public class UserPublisherPartDriver : ContentPartDriver<UserPublisherPart>
    {
        private readonly IUserPublisherService _userPublisherService;

        public UserPublisherPartDriver(IUserPublisherService userPublisherService)
        {
            _userPublisherService = userPublisherService;
        }

        protected override DriverResult Editor(UserPublisherPart part, dynamic shapeHelper)
        {
         
            return ContentShape("UserPublisher_Edit", () => shapeHelper.EditorTemplate(TemplateName: "UserPublisher", Model: BuildEditorViewModel(part)));

        }

        protected override DriverResult Editor(UserPublisherPart part, Orchard.ContentManagement.IUpdateModel updater, dynamic shapeHelper)
        {
            var viewModel = new UserPublisherViewModel();
            if (updater.TryUpdateModel(viewModel, Prefix, null, null))
            {
                _userPublisherService.UpdateFormForContentItem(part.ContentItem, viewModel);
            }
            return Editor(part, shapeHelper);

        }

        private UserPublisherViewModel BuildEditorViewModel(UserPublisherPart part)
        {
                
            var viewModel = new UserPublisherViewModel
            {
                PublisherName = part.PublisherName,
                Publishers = part.Publishers
            };

            return viewModel;
        }
    }
Developer
Apr 13 at 6:06 PM
You should, because you are passing in the Prefix value when performing the model binding.
Apr 13 at 7:42 PM
Thank you so much! I have been stuck on this for a couple of days. I really appreciate you taking the time.
Developer
Apr 14 at 9:28 AM
My pleasure!