0

I've got main model like this

 public class MainModel
    {
        public Child1 Child1 { get; set; }
        public Child2 Child2 { get; set; }
        public Child3 Child3 { get; set; }
        public Child4 Child4 { get; set; }
    }

These all are rendered as follows:

<form id="MainForm">
 @Html.Partial("~/Views/Child1.cshtml", Model.Child1)
 @Html.Partial("~/Views/Child2.cshtml", Model.Child2)
 @Html.Partial("~/Views/Child3.cshtml", Model.Child3)
 @Html.Partial("~/Views/Child4.cshtml", Model.Child4)
</form>

I'm trying to post mainform and the action method looks like this

[HttpPost]
public ActionResult Save(MainModel obj)
{

}

Everytime each object (Child1, Child2 etc.) are null. I'm posting form via AJAX as follows:

$.ajax({
        url: '@Url.Action("Save","SaveController")',
        type: "POST",
        data:$("#MainForm").serialize(),
        success: function (data) {
        }
     });

Am i missing something? Is there any other way i can process data? I can not have multiple submits on partial views that's the constraint here.

Any suggestions please.

Manish Dhariwal
  • 61
  • 1
  • 1
  • 9
  • Use an `EditorTempate` (your views need to be in the `/Views/Shared/EditorTemplates` folder) and `EditorFor(m =>m.Child1)` etc (not `Partial()`) so that the `name` attributes of your controls are generated with the correct prefix (or as an alternative, refer [this answer](https://stackoverflow.com/questions/29808573/getting-the-values-from-a-nested-complex-object-that-is-passed-to-a-partial-view/29809907#29809907) –  Jul 11 '18 at 06:57
  • @StephenMuecke : Thank you for the response. The method shown in the link you provided worked for me. Thanks a lot. however with this approach, i'm not able to identify the null objects. E.g. when i load form for the first time i pass new MainModel() which shows all the child objects as empty. Do we have any way by which i can identify what are the partial views which have data. Right now, if i simply serialize the form, all the models shows properties with default values. Is there any way by which we can have null objects for child in case it's not filled up? – Manish Dhariwal Jul 11 '18 at 10:32
  • Sorry, I do not understand what your mean - if you generate a form control for an object, it will never be `null` - but why would you include it if you want it to be `null`? (and as I noted, the correct approach is to use an `EditorTemplate`). –  Jul 11 '18 at 10:36
  • @StephenMuecke : Basically i'm working with multi step form which can be saved to be filled later. I show hide parts of the form. Now, if user fills two parts(Partial views) and save the form, i need to identify the parts which are left to be filled. – Manish Dhariwal Jul 11 '18 at 10:55
  • You should be only including the html that is needed in a step, and you either submit, save the current step and redirect to the next, or you use ajax to include the next step in the current view as required –  Jul 11 '18 at 10:58
  • Are the `name` of the fields correctly generated ? If they are null, it's because Razor fails to bind the value of your field with the (sub)property. A quick and dirty solution would be to pass `Model` to you child views, and this should work nicely. – AlexB Jul 11 '18 at 14:29
  • @ManishDhariwal were you able to figure out the problem? If so, how did you resolve? – blue piranha May 28 '19 at 16:56

0 Answers0