Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpPost on ASP.Net MVC3 - "No parameterless constructor defined for this object" [duplicate]

Tags:

c#

asp.net-mvc

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?

like image 427
TZHX Avatar asked Mar 25 '12 18:03

TZHX


1 Answers

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.

like image 61
Jeff Siver Avatar answered Nov 15 '22 15:11

Jeff Siver