views:

152

answers:

1

i see there are solutions using different model binders like castl but i didn't know if the basic default model binder supported nested complex objects when i am posting a form to a controller.

+2  A: 

I think you can if I understand your question.

In the name of my field I not only put the property name but object as well.

So if I have a "Person" object that contains an "Address" object that contains a "State" field I would have as the name "Person.Address.State" and that seems to resolve just fine in my controller.

<%= Html.TextBox("Person.Address.State", Person.Address.State....

Is this what you are asking?

EDIT

It does work and here is the code to get it to work.

namespace DoMyWork.Controllers
{
    public class test
    {
        public string value { get; set; }
    }

    public class testParent
    {
        public test test { get; set; }
    }

    public class SearchController : Controller
    {
        public ActionResult ViewUserControl1(testParent test)
        {
            UpdateModel(test);

            return View(test);
        }

SNIP

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<DoMyWork.Controllers.testParent>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    ViewUserControl1
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>ViewUserControl1</h2>

    <% using( Html.BeginForm()){ %>
        <%= Html.TextBox("test.value", Model.test.value) %>
        <input type="submit" value="sdf" />
    <%} %>

</asp:Content>
griegs
i think you understand my question but this doesn't seem to work. the complex object always shows up null . .
ooo
Yeah I thought the downvote was a little weird. Thanks @Jay.
griegs
+1 Didn't even realise you could do this, can't believe I've missed this after months of development. Is this a MVC2 feature or was this in the original MVC1 release?
Jay
MVC 1. I haven't even touched MVC 2 yet. Yeah I know. :)
griegs
ah . .i think the issue may have something to do with the fact that i am generating my controls on the fly with jquery . .somehow the values dont show up in the form.
ooo
If you generate your controls in jQuery then perhaps you should consider returning a partial view and rendering that. Then the controls will have the values because you are passing your partial view your complex model. Forgive me if that is already what you are doing
griegs
i am allowing the user to select from a dropdown and that generates a input textbox on the fly . . i do see this value in the FormCollection but its just null in the object directly . .
ooo
Hmmm, tricky. Perhaps you could start a new question with this fresh information and post some of the pertinent code. I think this question may be getting a little stale now.
griegs
agree . . i actually figured it out . . i didn't have a {get;set;} on the child object . . so foolish . .
ooo
Bingo. Can't count the number of times i did that also. Well done
griegs
i will accept your answer as it correctly answered my problem. i will also remove the jquery stuff as that was a non issue
ooo