Help me to understand. Generated crud controller. I have a collection in the model, so I want the input fields for the elements of this collection to be added on the creation page by button.
Tried to do on the guide http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
Now, instead of adding an input field, throws it onto a separate page with this input field.
Checklist.cs - my model
public class Checklist { public int Id { get; set; } public string Identifier { get; set; } public IEnumerable<Checks> CheckList { get; set; } public DateTime Date { get; set; } } public class Checks { public int Id { get; set; } public string Check { get; set; } } Create.cshtml
@using (Html.BeginForm()) { @Html.AntiForgeryToken() <h4 align="right">New Checklist</h4> @Html.TextBoxFor(model => model.Identifier, new { @class = "form-control", @maxlength = "15", @style = "width:280px" }) @Html.ValidationMessageFor(model => model.Identifier, "", new { @class = "text-danger" }) @Html.TextBoxFor(model => model.Title, new { @class = "form-control", @maxlength = "255" }) @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" }) <table id="Table"> <tr> <th>Checks</th> </tr> <tr> <td id="editorRows"> @if (Model.CheckList != null) { foreach (var item in Model.CheckList) { @Html.Partial("_create_check", item); } } </td> </tr> </table> @Html.ActionLink("Add", "BlankEditorRow", null, new { id = "addItem" }) <input type="submit" value="Create" class="btn btn-primary" /> } add-check.js
$('#addItem').click(function () { $.ajax({ url: this.href, cache: false, success: function (html) { $("#editorRows").append(html); } }); return false; }); _create_check - partial view
@model MyProject.Models.Checks @using (Html.BeginCollectionItem("checks")) { @Html.HiddenFor(model => model.Id) @Html.TextBoxFor(model => model.Check, new { @class = "form-control" }) } ChecklistsController.cs
public ViewResult BlankEditorRow() { return View("_create_check", new Checks()); } public ActionResult Create() { Checklist checks = new Checklist(); return View(checks); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "Id,Identifier,Title,Date,CheckList")] Checklist checklist) { if (ModelState.IsValid) { checklist.Date = DateTime.Now; db.Checklists.Add(checklist); db.SaveChanges(); return RedirectToAction("Index"); } return View(checklist); }