MVC3 Master Detail Create

MVC3 Master Detail Create

Old forum URL: forums.lhotka.net/forums/t/10284.aspx


fark posted on Tuesday, April 19, 2011

I am getting a "No parameterless constructor defined for this object.Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

I have a MVC3 website that has a parent object named "Parent".  This parent has a collection of "Child1" objects named "Child1List".

I have a page called Create.cshtml where I type in the value for my parent field and my child field.

@model  ParentChildApplication.ViewModels.ParentViewModel

@{
    ViewBag.Title = "Create";
}


        <h2>Customer Create</h2>

        @using(Html.BeginForm())
  {
                @Html.ValidationSummary(false)

                <fieldset>
                        <legend>Fields</legend>
                                               
                        <div class="editor-label">
                                @Html.LabelFor(model => model.ModelObject.Description)
                        </div>
                        <div class="editor-field">
                                @Html.TextBoxFor(model => model.ModelObject.Description)
                                @Html.ValidationMessageFor(model => model.ModelObject.Description)
                        </div>

                        <fieldset>
                        @for (int i = 0; i < Model.ModelObject.Child1List.Count; i++)
                        {
                        <div class="editor-label">
                                @Html.LabelFor(model => Model.ModelObject.Child1ListIdea.Description)
                        </div>
                        <div class="editor-field">
                                @Html.TextBoxFor(model => Model.ModelObject.Child1ListIdea.Description)
                                @Html.ValidationMessageFor(model => Model.ModelObject.Child1ListIdea.Description)
                        </div>
                        }
                        </fieldset>


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

When I click save I get the error mentioned above.

Just for fun, when I change:

private Child1()
        {
            // Prevent direct creation
        }

to

public Child1()
        {
            // Prevent direct creation
        }

I get the error:

"List item must be marked as a child object"   "Exception Details: System.InvalidOperationException: List item must be marked as a child object"

I don't know anything about MVC, I am just starting, but it seems like it is trying to instantiate a child object but is not using AddNew().  Is there anyway to correct this behavior or fix this problem?

Thanks,

Mark

RockfordLhotka replied on Wednesday, April 20, 2011

Look at the MVC UI project in the SimpleNTier solution.

Odds are you aren't using the CslaModelBinder, or the Controller class from Csla.Web.Mvc. Together, these two types work to make MVC work well with CSLA business objects.

fark replied on Wednesday, April 20, 2011

Yes you are right.  I changed the Order controller just to test that it has the ability to create child objects when it is creating parents and it works.  I changed this:

    public ActionResult Create()
    {
        BusinessLibrary.Order order = BusinessLibrary.Order.NewOrder();
        BusinessLibrary.LineItem lineitem = order.LineItems.AddNew();
        lineitem.Name = "TEST";
        ViewData.Model = order;
      return View();
    }

in the SimpleNTier. When I submit the create the parent and child are both created.
 Now I need to determine what I am doing wrong.  My CSLA is generated with CSLAGenFork. 
The generated CSLA that I have is organized quite differently than SimpleNTier so I will have to look tonight and see how it is different.  Thanks for pointing me in the right direction.  I appreciate everything you do!

Mark

Copyright (c) Marimer LLC