No parameterless constructor defined for this object, after form with ID

I'm working on a project and I'm not used to C#. I tried to work after my old working code. I couldn't find any difference.

My html form:

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.TicketID)
    <fieldset>
        <legend>Ticketdetail</legend>

          <div class="editor-label">
            @Html.LabelFor(model => model.Anmerkung)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Anmerkung)
            @Html.ValidationMessageFor(model => model.Anmerkung)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

the actions:

public ActionResult CreateDetail(int id)
{
    if (id == -1) return Index(-1);
    return View(new cTicketDetail(id, User.Identity.Name));
}

[HttpPost]
public ActionResult CreateDetail(cTicketDetail collection)
{


    //int TicketID = collection.TicketID;
    try
    {
        if (ModelState.IsValid)
        {
            collection.Write();
        }
        return RedirectToAction("Details", collection.TicketID);
    }
    catch
    {
        return this.CreateDetail(collection.TicketID);
    }
}

the error after commiting my Form

Answers


It looks like the cTicketDetail type that you have used in your CreateDetail action doesn't have a parameterless constructor. Controller actions cannot take such types as arguments because the default model binder doesn't know how to instantiate them.

The best practice here is to define a view model and then have your controller action take this view model as parameter instead of using your domain entities.

If you don't want to use view models you will have to modify your cTicketDetail type so that it has a default constructor:

public class cTicketDetail
{
    // The default parameterless constructor is required if you want
    // to use this type as an action argument
    public cTicketDetail()
    {
    }

    public cTicketDetail(int id, string username)
    {
        this.Id = id;
        this.UserName = username;
    }

    public int Id { get; set; }
    public string UserName { get; set; }
}

Need Your Help

Combo box only displaying one record

excel vba combobox

I have a combobox in Excel VBA that it only displaying one record but it should display 7. I have debuged the code and I can see how the routine goes throught the code assigning all the entries in ...

App crashes at

iphone objective-c ios4 uiwebview

I am implementing a simple in-app browser. In my home view (UITableViewController), I have something like:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.