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
2009-12-11 02:04:42
i think you understand my question but this doesn't seem to work. the complex object always shows up null . .
ooo
2009-12-11 02:17:47
Yeah I thought the downvote was a little weird. Thanks @Jay.
griegs
2009-12-11 02:33:59
+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
2009-12-11 02:35:55
MVC 1. I haven't even touched MVC 2 yet. Yeah I know. :)
griegs
2009-12-11 02:37:11
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
2009-12-11 02:47:48
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
2009-12-11 02:50:58
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
2009-12-11 02:57:05
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
2009-12-11 02:58:50
agree . . i actually figured it out . . i didn't have a {get;set;} on the child object . . so foolish . .
ooo
2009-12-11 03:22:53
Bingo. Can't count the number of times i did that also. Well done
griegs
2009-12-11 03:23:31
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
2009-12-11 03:23:34