C# Razor beginform binding nested object in view and post it all back to controller
I have a view model like this:
namespace MySystem.Models.View { public class ItemPage { public Item Item { get; set; } public int CurrentItemID { get; set; } public string ModalHeading { get; set; } public string SubmitButtonText { get; set; } /// <summary> /// This will be the color that we will use in the view, if set /// </summary> public System.Drawing.Color CssColorTwist { get; set; } public ItemPage() { //ItemEngine iEngine = new ItemEngine(); //this.Item = iEngine.GetItem(CurrentItemID); } public ItemPage(Item item) { this.Item = item; this.CurrentItemID = Item.ItemID; } public ItemPage(Item item, string btnText, string modalHeading) { this.Item = item; ModalHeading = modalHeading; SubmitButtonText = btnText; this.CurrentItemID = Item.ItemID; } public ItemPage(Item item, string btnText, string modalHeading, System.Drawing.Color cssColor) { this.Item = item; ModalHeading = modalHeading; SubmitButtonText = btnText; CssColorTwist = cssColor; this.CurrentItemID = Item.ItemID; } public Color GetCssColorDarker(double factor) { if(CssColorTwist != null) { return CssColorTwist.GetColorDarker(factor); } return new Color(); } public Color GetCssColorLighter(double factor) { if (CssColorTwist != null) { return CssColorTwist.GetColorLighter(factor); } return new Color(); } }}
When posting to the controller, all the properties of the view model end up as null. If I remove the nested object Item from the view model and remove all bindings to that nested object in the view, then all works good, all other properties are set.
So the problem is, how to bind nested object properties in the Item object in the view and post back to the controller with the model and nested object and all?
ItemPage shall already have been initialized when received by the controller method with the values input by the user in the view via the bindings there.
The whole view looks like this. Bare in mind that this is only a test under construction. I have squared the name with a red box in the picture.
There is no error message. As stated in the question, all properties of the model end up as null when received by the controller (verified via debugging at runtime in Visual Studio) unless I remove the Item object bindings from the view and remove the Item object from the model. In short; it works, the properties are populated by the user input, as long as I don't use a nested object as a property (the Item object). But that is what I want to do.
Open in new window
ItemPage shall already have been initialized when received by the controller method with the values input by the user in the view via the bindings there.The whole view looks like this. Bare in mind that this is only a test under construction. I have squared the name with a red box in the picture.
There is no error message. As stated in the question, all properties of the model end up as null when received by the controller (verified via debugging at runtime in Visual Studio) unless I remove the Item object bindings from the view and remove the Item object from the model. In short; it works, the properties are populated by the user input, as long as I don't use a nested object as a property (the Item object). But that is what I want to do.