Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC SelectList in ViewModel
    primarykey
    data
    text
    <p>I'm working in ASP.NET MVC 5 (but this most likely applies to previous versions also). Best way to ask this question is to show you the code:</p> <p>Here is the View Model:</p> <pre><code>public class PersonCreateViewModel { public SelectList cities {get; set;} public String Name { get; set; } public String Address { get; set; } } </code></pre> <p>Here is the http Post method from the controller:</p> <pre><code>[HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(PersonCreateViewModel viewmodel) { if (ModelState.IsValid) { //Add to database here and return } //return back to view if invalid db save return View(person); } </code></pre> <p>Here is the View:</p> <pre><code>&lt;div class="form-group"&gt; @Html.LabelFor(model =&gt; model.person.name, new { @class = "control-label col-md-2" }) &lt;div class="col-md-10"&gt; @Html.EditorFor(model =&gt; model.person.name) @Html.ValidationMessageFor(model =&gt; model.person.name) &lt;/div&gt; &lt;/div&gt; &lt;div class="form-group"&gt; @Html.LabelFor(model =&gt; model.person.address, new { @class = "control-label col-md-2" }) &lt;div class="col-md-10"&gt; @Html.EditorFor(model =&gt; model.person.address) @Html.ValidationMessageFor(model =&gt; model.person.address) &lt;/div&gt; &lt;/div&gt; &lt;div class="form-group"&gt; @Html.LabelFor(model =&gt; model.person.CityID, "CityID", new { @class = "control-label col-md-2" }) &lt;div class="col-md-10"&gt; @Html.DropDownList("cities") @Html.ValidationMessageFor(model =&gt; model.person.CityID) &lt;/div&gt; &lt;/div&gt; </code></pre> <p>When the user clicks submit, the following error message is in the browser: "No parameterless constructor defined for this object. "</p> <p>I think it has something to do with the fact that I have a SelectList in my ViewModel. I think when the view passes the model back to the controller on form submission, it calls the constructor for the SelectList, but there is no parameterless constructor for SelectList. I'm not sure how to proceed. Any help is appreciated!!</p>
    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.
    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