Populating ListBoxfor with ViewModel and passing b

2019-08-05 10:29发布

问题:

I am struggling to figure out how to pass back my model (unmodified) back to my controller. I have tried binding my ViewModel to many different HTML.Helpers and what not. I can get my data to bind and display, but I cannot get the ViewModel to post back to my controller.

I have the following ViewModels:

public class MediaProviderViewModel : ViewModelBase
{
    public int MediaProviderId { get; set; }
    public string MediaProviderName { get; set; }

    public static MediaProviderViewModel FromEntity(MediaProvider entity)
    {
        if (entity == null)
        {
            return (null);
        }
        return (new MediaProviderViewModel()
        {
            MediaProviderId = entity.MediaProviderId,
            MediaProviderName  = entity.Name,

        });
    }

    public static IEnumerable<MediaProviderViewModel> FromEntityList(IEnumerable<MediaProvider> entities)
    {
        return entities == null ? null : entities.Select(FromEntity).OrderBy(mp => mp.MediaProviderName);
    }
}


  public class SystemConfigViewModel : ViewModelBase
{
    [DisplayName("Media Providers")]
    public IEnumerable<MediaProviderViewModel> MediaProviders { get; set; }

    public SystemConfigViewModel()
    {
    }

    public static SystemConfigViewModel FromEntity(Entities db)
    {
        return new SystemConfigViewModel()
            {
                MediaProviders = MediaProviderViewModel.FromEntityList(db.MediaProviders)
            };
    }    
}

Controller:

public class SystemConfigController : CommonBaseController
{
    //
    // GET: /SystemConfig/

    public ActionResult Index()
    {
        SystemConfigViewModel model = SystemConfigViewModel.FromEntity(_db);
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(SystemConfigViewModel model)
    {

        return View(model);
    }

View:

<html>
  <body>
    @using (Html.BeginForm("Index", "SystemConfig"))
    {    
        @Html.ListBoxFor(model => model.MediaProviders, new SelectList(Model.MediaProviders, "MediaProviderId", "MediaProviderName"), new { @size = "30" })       
    }
  </body>
</html>

I have tried iterating through the items and indexing them like such:

//Surrounded in a for loop with different helpers

model => model.MediaProviders[i].MediaProviderId

Still nothing is passed back. Could I get some help as to what I am doing wrong? I tried googling but nothing really of value popped up for my case.

回答1:

You should include an additional property in you view model to bind the selected values to (and changing the MediaProviders to a SelectList would also make it easier)

public class SystemConfigViewModel : ViewModelBase
{
  public IEnumerable<int> SelectedProviders { get; set; } //bind to this    
  public SelectList MediaProviders { get; set; } // populate this in the controller
}

Then in the view

@Html.ListBoxFor(m=> m.SelectedProviders , Model.MediaProviders, new { @size = "30" })

When you post back, the SelectedProviders property will contain the ID's (MediaProviderId) of the selected items.