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?

Community
  • 1
  • 1
TZHX
  • 5,291
  • 15
  • 47
  • 56

1 Answers1

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.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Jeff Siver
  • 7,434
  • 30
  • 32
  • so any information in the model that's not in the form is lost in the new model? – TZHX Mar 25 '12 at 18:29
  • 2
    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. – Jeff Siver Mar 25 '12 at 18:30
  • But... I've got that are just displayed as Labels that aren't being transfered over. :/ – TZHX Mar 25 '12 at 18:37
  • 3
    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. – Jeff Siver Mar 25 '12 at 20:16