I'm a bit of an MVC newbie, so you'll have to forgive what I imagine is an elementary question.
I created a custom viewmodel in order to have a multiselect list in my form:
public class CustomerFormViewModel
{
public Customer Customer { get; private set; }
public MultiSelectList CustomerType { get; private set; }
public CustomerFormViewModel(Customer customer)
{
Customer = customer
// this returns a MultiSelectList:
CustomerType = CustomerOptions.Get_CustomerTypes(null);
}
}
I found that my first attempt only captured the first value of the multiselect, and I guessed that this is because my create actions looked like this:
// GET: /Buyer/Create
public ActionResult Create() { ... }
// POST: /Buyer/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Customer customer) { ... }
So, I decided to change it to this:
// GET: /Buyer/Create
public ActionResult Create() { ... }
// POST: /Buyer/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(CustomerFormViewModel model) { ... }
So that I can get the full output from the MultiSelectList and parse it accordingly. Trouble is, this complains that there's no parameterless constructor for the viewmodel (and there isn't) - and I'm not sure the right way to go about fixing this. Nothing I've tried has worked and I really need some help!
In case it helps, my view looks like this:
<%@ Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MySite.Controllers.CustomerFormViewModel>" %>
...
<% using (Html.BeginForm())
<%= Html.ListBox("CustomerType", Model.CustomerType)%>
...
Thanks!