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.

leora
  • 188,729
  • 360
  • 878
  • 1,366
  • BTW @oo, I think your object is showing up null because you may not be exposing the child objects as public. Had the same issue when I ran some tests a minute ago and deliberatley set the child objects to private. That may explain your null value. – griegs Dec 11 '09 at 02:40

2 Answers2

3

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
  • 22,624
  • 33
  • 128
  • 205
  • i think you understand my question but this doesn't seem to work. the complex object always shows up null . . – leora Dec 11 '09 at 02:17
  • Yeah I thought the downvote was a little weird. Thanks @Jay. – griegs Dec 11 '09 at 02:33
  • +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 Dec 11 '09 at 02:35
  • MVC 1. I haven't even touched MVC 2 yet. Yeah I know. :) – griegs Dec 11 '09 at 02:37
  • 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. – leora Dec 11 '09 at 02:47
  • 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 Dec 11 '09 at 02:50
  • 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 . . – leora Dec 11 '09 at 02:57
  • 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 Dec 11 '09 at 02:58
  • 1
    agree . . i actually figured it out . . i didn't have a {get;set;} on the child object . . so foolish . . – leora Dec 11 '09 at 03:22
  • Bingo. Can't count the number of times i did that also. Well done – griegs Dec 11 '09 at 03:23
  • i will accept your answer as it correctly answered my problem. i will also remove the jquery stuff as that was a non issue – leora Dec 11 '09 at 03:23
0

I had the same problem and I found a similar question that helped a lot of people to solved this: Complex object and model binder ASP.NET MVC, but it didn't solve my problem.

I realized that the problem was caused because my inputs were disabled (I was disabling them with JQuery on document ready) as you can see it here: How do I submit disabled input in ASP.NET MVC?. At the end I used read-only instead of disabled attribute.

Community
  • 1
  • 1
joalcego
  • 1,098
  • 12
  • 17