1

Possible Duplicate:
Complex models and partial views - model binding issue in MVC3

Can you pass data from a View to a Controller when the properties are sitting within a class that is within the Model used in that View?

The problem is that when the Submit happens, it seems that properties of my Model that belong to a class within that Model do not get populated. Properties that are not in classes DO get populated. Just think there is a instantiation problem here but not seeing the path forward.

Here is the Model setup:

namespace AppName.Models
{
    public class SuperModel
    {
        public SuperModel() {
            SubModA = new SubModelA();
        }
        public string myItem1 { get; set; }
        public SubModelA SubModA { get; set; }
    }

    public class SubModelA
    {
        public string mySubAItem1 { get; set; }
    }
}

Here is the View:

@model AppName.Models.SuperModel

@{
    ViewBag.Title = "My Page Title";
}

<h2>My Page Title:</h2>

@using (Html.BeginForm("NextController", "NextControllerFolder", new { SuperModel = Model }, FormMethod.Post))
{
    @{
        Model.myItem1 = DateTime.Now.ToShortDateString();
        Model.SubModA.mySubAItem1 = DateTime.Now.ToShortDateString();
    }
    @Html.HiddenFor(m => m.myItem1)
    @Html.HiddenFor(m => m.SubModA.mySubAItem1)

    <p>
        <button name="submit" value="Submit"><b>Continue</b></button>
    </p>
}

Here is the Controller:

[HttpPost]
public ActionResult NextController(string button, SuperModel model, string returnUrl)
{
    // PROBLEM IS HERE>>>
    ///model.myItem1 has a value equal to the current date
    // model.SubModA.mySubAItem1 is null

    return(model);
}

Hope this question made sense. Hope the answer is just around the corner! Thanks!

Community
  • 1
  • 1
  • If I read this correctly -- and it's a bit hard to read -- you are asking about passing Complex Types into a controller specifically when contained within another Complex Type. It's asked a lot on SO, see if this helps http://stackoverflow.com/questions/1934160/trouble-passing-complex-data-between-view-and-controller-in-asp-net-mvc – inevio Aug 12 '12 at 01:53
  • This code doesn't compile. Could you put your real code? I test it with some changes (for example: I removed the second `@{}` in the view and change `Model.SubModelA.mySubAItem1` for `Model.SubModA.mySubAItem1`) and it works. Maybe in your real code you omitted some part that you put in you example code. – Pablo Claus Aug 12 '12 at 03:48
  • You are passing the Type of the property, SubModelA, while you should pass the name of the property that you defined: that is SubModA. – Rajesh Aug 12 '12 at 04:32
  • Thanks all. I corrected the typo in the sample code above. The real code is correct in that regard. Sorry! In reading other posts & docs, the DefaultModelBinder *should* recurse my model and have no problem with the bindings. Others seem to go toward using a custom model binder if that have any trouble. But my hope was to avoid that since a custom model binder would have to support the recursion encountered with a complex models like this. – Eric Geerdes Aug 12 '12 at 20:50
  • @matthewnreid, Thanks, but in looking at that, the focus was on binding problems associated with a radio-button. Not seeing how that one applies to this, except for workarounds on adding a controller parameter, or reloading the object from the underlying data service. – Eric Geerdes Aug 12 '12 at 20:56

2 Answers2

0

Try this:

 @Html.HiddenFor(m => m.SubModA.mySubAItem1)

will work.

Rajesh
  • 1,459
  • 10
  • 17
0

Replace following line in your code

@Html.HiddenFor(m => m.SubModelA.mySubAItem1)

with this one

@Html.HiddenFor(m => m.SubModA.mySubAItem1)

Hope this help!

Display Name
  • 4,672
  • 1
  • 33
  • 43