Jani
Jani

Reputation: 11

Strongly typed Dataset model binding in ASP.NET MVC

In my latest project I have to use strongly typed DataSets. I have a problem submit a form to a POST controller: I get an parameterless constructor error i.e. it does not found the OrderCreate post method on controller. I will appriciate any help. Thank you.

Here is my simplified code:

View:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyPortal.Models.MyTypedDataRow >" %>
<% using (Html.BeginForm("OrderCreate", "Order", FormMethod.Post))
{%>
    <%: Html.ValidationSummary(true)%>
    <fieldset>
        <legend>Fields</legend>

        <p class="fields"> 
            <%: Html.Label("Customer")%>
            <%: Html.TextBoxFor(model => model.CustomerName)%>
            <%: Html.ValidationMessageFor(model => model.CustomerName)%>
        </p>
        <p>
            <input type="submit" value="Add" />
        </p>
     </fieldset>

<% } %>

Controllers:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult OrderCreate()
{
    MyTypedDataTable table = new MyTypedDataTable ();
    MyTypedDataRow row = table.NewMyTypedDataRow();

    return PartialView(row);
}


[AcceptVerbs(HttpVerbs.Post)]
public ActionResult OrderCreate(FormCollection coll, MyTypedDataRow row)
{   
    int result = m_repo.InsertGI(row);
    if (result > 0)
    {
        return RedirectToAction("OrderList");
    }
    else
    {
      return View("Error");
    }
}

Upvotes: 1

Views: 2162

Answers (2)

queen3
queen3

Reputation: 15501

  1. Write a custom data binder that can create MyTypedDataRow. You can write a generic binder that will know how to create all your non-default objects.
  2. Create MyTypedDataRow in action and then call UpdateModel() manually.
  3. Create additional MyTypedDataRowViewModel that will copy MyTypedDataRow fields (but can also add validation attributes, more view logic, etc) and use AutoMapper to convert between them. Pass MyTypedDataRowViewModel to the POST action.

Upvotes: 0

andymeadows
andymeadows

Reputation: 1336

I would guess that you have no parameterless constructor on MyTypedDataRow. The model binder requires a parameterless constructor to populate the item. You can take all of the properties individually into the method and construct the row using the table's NewMyTypedDataRow method or you can write a custom data binder, but without a parameterless constructor you will be unable to do what you're trying to do with the default model binder.

Upvotes: 1

Related Questions