Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC custom viewmodel problems
    primarykey
    data
    text
    <p>I'm a bit of an MVC newbie, so you'll have to forgive what I imagine is an elementary question.</p> <p>I created a custom viewmodel in order to have a multiselect list in my form:</p> <pre><code>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); } } </code></pre> <p>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:</p> <pre><code> // GET: /Buyer/Create public ActionResult Create() { ... } // POST: /Buyer/Create [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(Customer customer) { ... } </code></pre> <p>So, I decided to change it to this:</p> <pre><code> // GET: /Buyer/Create public ActionResult Create() { ... } // POST: /Buyer/Create [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(CustomerFormViewModel model) { ... } </code></pre> <p>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!</p> <p>In case it helps, my view looks like this:</p> <pre><code>&lt;%@ Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;MySite.Controllers.CustomerFormViewModel&gt;" %&gt; ... &lt;% using (Html.BeginForm()) &lt;%= Html.ListBox("CustomerType", Model.CustomerType)%&gt; ... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload