7

Possible Duplicate:
ASP.NET MVC: No parameterless constructor defined for this object

I'm working on an ASP.NET MVC3 application.

I'm trying to use the [HttpPost] to retrieve information when a user enters it on a form.

Basing what I do off the "default" blank ASP.Net project's Logon scripts, I have the following:

In my controller:

    public ActionResult Ticket(int id)
    {
        Models.Ticket model = new Models.Ticket(id);
        return View("Ticket", model);
    }

    [HttpPost]
    public ActionResult Ticket(int id, MMCR.Models.Ticket model)
    {
        if (id != model.TicketNo)
        {
            return View("Error");
        }
        return View("Ticket", model);
    }

And in the View I have:

@using (Html.BeginForm()) {
    <div>
    <fieldset>
    <legend>View Ticket Details</legend>

    <div class="editor-label">
        @Html.LabelFor(m=>m.Status)    
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(m=>m.Status, Model.Status)
    </div>

    <p>
        <input type="submit" value="Update" />
    </p>

    </fieldset>
    </div>
}

(obviously snipping out repetetive stuff).

However, when I click on the button I get an error:

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

Can anyone give some advice on how to resolve this?

5
  • 1
    Which object is missing the parameterless constructor? If it's one of yours, you can take a stab in the dark and add a parameterless constructor to it. Commented Mar 25, 2012 at 18:14
  • None of the stuff in the Stack Trace is from one of my classes. :( Commented Mar 25, 2012 at 18:17
  • stackoverflow.com/q/1355464/102937 Commented Mar 25, 2012 at 18:19
  • stackoverflow.com/q/3477518/102937 Commented Mar 25, 2012 at 18:20
  • does MMCR.Models.Ticket have a no-args ctor? Commented Mar 25, 2012 at 18:22

1 Answer 1

18

Your class MMCR.Models.Ticket needs a parameterless constructor.

When you pass an object of this type through the Post method, MVC will create an instance of the class using a parameterless constructor. It will then map the form fields to that object.

Sign up to request clarification or add additional context in comments.

4 Comments

so any information in the model that's not in the form is lost in the new model?
That is correct. If you have information you want to keep, you should add it to the view through Html.HiddenFor(x=>x.PropertyToKeep). That will insure it is populated when the form posts.
But... I've got that are just displayed as Labels that aren't being transfered over. :/
Labels don't get transfered because they aren't considered to be HTML controls. Only HTML controls are included; that's why you have to do a HiddenFor to get a value back if you don't have the property in another control.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.