I have this model that has a list of another model. Then, in my opinion, I have my form, which fills a couple of fields for my main model. But I want this form to also be able to add X-models of a different type and be connected to the network, and I'm wondering how to do it right.
So here are my two models:
public class MyMainModel { public int MyMainId { get; set; } [Required(ErrorMessage = "Groovy name required")] [Display(Name = "MyMainModel groovy name:")] public string Name { get; set; } public List<MySubModel> MySubModels { get; set; } } public class MySubModel { public int MySubId { get; set; } [Required(ErrorMessage = "Cool name required")] [Display(Name = "MySubModel cool name:")] public string Name { get; set; } }
When I hit my controller for my βcreationβ, I go through this action:
public ActionResult SomePageAboutCreating() {
Now it goes to my strongly typed view:
@model myFunProject.WebModels.MyMainModel <div> <form id="my-create-form" onsubmit="CreateMyMainModel(this); return false;"> @Html.AntiForgeryToken() @Html.ValidationSummary() <div class="result" style="background-color: silver;">This is the operation result box</div> <img class="loading" src="/Images/ajax-loader.gif" alt="Loading..." width="16" height="16" style="display: none;" /> <section> @Html.LabelFor(m => m.Name) @Html.TextBoxFor(m => m.Name) @Html.ValidationMessageFor(m => m.Name) </section> @Html.EditorFor(x => x.MySubModels) <input type="submit" class="btn-send" id="my-create-form-submit" value="Send" /> </form> </div>
So, I think I need to use EditorTemplates here ... Therefore, I configure in my /Views/EditorTemplates/MySubModels.cshtml (with the name of my property MyMainModel), and then when I write my form there, I get confused ...
@model myFunProject.WebModels.MyMainModel @*<section> @Html.LabelFor(m => m.Name) @Html.TextBoxFor(m => m.Name) @Html.ValidationMessageFor(m => m.Name) </section>*@
So, here I am not sure what to add here ... I want my Name property to be one of the "MySubModel". And since the user sees this form, let's say, for example, he will consider this scenario:
- Enters a name for "MyMainModel".
- Change to another name and fill in the name of the first instance of "MySubModel".
- Then he presses a special button that will manipulate dom to add another MySubModel.Name field.
- He will write in the second "MySubModel".
- He will click the Submit button.
The Ajax call that I put in there, I do the wiring normally, but my confusion comes with the code I have to write for the editor template, and then I also wonder how I am going to create a new field (for this second "MySubModel", for example ...).
Any help would be appreciated; I looked through a lot of articles about subjects close to this, but have not yet found this case. Thanks!
EDIT:
I will add an action (a too simplified version of hehe) that is called by my ajax when the form is submitted.
public ActionResult CreateMyMainModel(MyMainModel myMainModel) { // [...] Do stuff like save to database... // var aGroovyNAme = myMainModel.Name; foreach(var mySubModel in myMainModel.MySubModels) { // Here I would have the sub models available to manipulate... // var aCoolName = mySubModel.Name; } return Content("ok"); }