This helped me to get on the right path , but not completely what I wanted to do, and since it does not seem to be easily supported in MVC, here is my job to get all this working with binding the model to the controller.
My ultimate goal
To dynamically add child objects to the form with the corresponding html so that they automatically bind to the parent object when passed to the control method, that is, I needed HTML like this
<input name='Lead_Beneficiaries_1__{Property}' id=... />
And not so (this is what leads the blog post)
<input name='Beneficiary__{Property}' />
which does not auto-bind to the parent when sent to the controller.
Decision
Instead of using Ajax to visualize the html editor template from the controller, I automatically expose one editor template when the form loads, and then can be loaded as necessary by the user. JQuery code
$("#addBeneficiary").click(function(e) { e.preventDefault(); var beneficiary = $(".beneficiary:last").clone(); var count = parseInt(beneficiary.find("input:first").attr("id").match(/\d+/), 10); beneficiary.find("input").each(function(indx, element) { var name = $(element).attr('name').replace(count, count+1), id = $(element).attr('id').replace(count, count+1); $(element).attr('name', name); $(element).attr('id', id); $(element).val(''); }); $("#beneficiaries").append(beneficiary); });
I just wrapped my editorial template in a div so that it could be easily grabbed as an integer from jquery, clone it, clear input values โโand increase the counter for each element, so when I publish, I get a set of child objects on my parent without the need to explicitly catch them in method or pull out of the dictionary form.
Although this is probably not the best solution, it does the job as I expected, and I did not duplicate the HTML for the editor template. Also, if there are form errors, since the parent has several children, they are automatically generated using my form code and nothing is lost due to client-side additions.