10

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:

Here is the View Model:

public class PersonCreateViewModel
{
    public SelectList cities {get; set;}
    public String Name { get; set; }
    public String Address { get; set; }

}

Here is the http Post method from the controller:

[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);
}

Here is the View:

<div class="form-group">
        @Html.LabelFor(model => model.person.name, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
        @Html.EditorFor(model => model.person.name)
        @Html.ValidationMessageFor(model => model.person.name)
    </div>
</div>

<div class="form-group">
        @Html.LabelFor(model => model.person.address, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
        @Html.EditorFor(model => model.person.address)
        @Html.ValidationMessageFor(model => model.person.address)
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(model => model.person.CityID, "CityID", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("cities")
        @Html.ValidationMessageFor(model => model.person.CityID)
    </div>
</div>

When the user clicks submit, the following error message is in the browser: "No parameterless constructor defined for this object. "

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!!

8
  • Full stack trace? There's no reason why the SelectList constructor would be called here. Commented Dec 4, 2013 at 22:16
  • Also, is that your full view model? Are you sure there isn't a constructor with parameters on it? Commented Dec 4, 2013 at 22:19
  • Person class has parameterless construstor? Commented Dec 4, 2013 at 22:20
  • In addition, you are only passing back the person to the view if the model state is invalid. You are not resetting/repopulating the select list (this full select list does not come back with the model). This could throw the error you are seeing since you are setting a Html Helper with a null reference. Commented Dec 4, 2013 at 22:20
  • Sorry, I made a mistake. I broke out the Person class in the view model, so there is no Person class in the viewmodel. Commented Dec 4, 2013 at 22:45

1 Answer 1

22

I've always had better luck using IEnumerable

public class PersonCreateViewModel
{
    public IEnumerable<SelectListItem> cities {get; set;}
    public int CityId { get; set; }
    public String Name { get; set; }
    public String Address { get; set; }
}

Also, you are going to need a property on the view model to capture the selected value like CityId.

Then you can use:

Html.DropDownListFor(m => m.CityId, Model.cities)
Sign up to request clarification or add additional context in comments.

4 Comments

Hate to say this but I'm lost. I Now have this: public IEnumerable<SelectListItem> cities { get; set; } and I need to populate it before it gets passed into the view. I used to have this in my controller: viewmodel.cities = new SelectList(db.Cities, "CityID", "name"); Now that it's an IEnumerable, how can I populate the drop down. Sorry, but my c# syntax skills aren't that great yet
I believe that SelectList implements IEnumerable<SelectListItem> so you should be able to assign a SelectList directly to an IEnumerable<SelectListItem>. For example, viewmodel.cities = new SelectList(db.Cities, "CityID", "name") should still work.
Is there a difference between IEnumerable and List ?
List implements IEnumerable, basically use lists if you can, if not use IEnumerable. (Because Entity framework will spam your database with IEnumerable)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.