Мне нужна коррекция по приведенному ниже коду. У меня есть 2 класса «Сотрудник» и «Ребенок». Когда я хочу создать нового Сотрудника, я хотел бы иметь возможность создать в той же форме связанного с ним ребенка (максимум 2 ребенка).ASP.NET MVC 5: Редактирование нескольких записей в привязке вида и модели (Обновление данных)
Ниже приведены модели
public class Employee
{
public int EmployeeID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int ChildID { get; set; }
public virtual ICollection<Child> Childs { get; set; }
}
public class Child
{
public int ChildID { get; set; }
public string NameChild { get; set; }
public string SurnameChild { get; set; }
public virtual Employee Employee { get; set; }
}
Контроллер Сотрудник
public class EmployeController : Controller
{
private ComideContext db = new ComideContext();
// GET: Employe/Create
public ActionResult Create()
{
List<Child> model = new List<Child>();
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "EmployeID,Name,Surname,ChildID")] Employee employee)
{
if (ModelState.IsValid)
{
db.Employes.Add(employe);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(employe);
}
}
Взгляд формы Сотрудника
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Employe</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Surname, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Surname, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Surname, "", new { @class = "text-danger" })
</div>
</div>
@for (int i=0; i<2; i++)
{
<div class="form-group">
@Html.LabelFor(model => model.NameChild, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.NameChild, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.NameChild, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SurnameChild, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.SurnameChild, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.SurnameChild, "", new { @class = "text-danger" })
</div>
</div>
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
Любая помощь/мысли были бы весьма признательны.
спасибо.
Некоторые опции для динамического добавления и удаления элементов коллекции [здесь] (http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796 # 29161796) и [здесь] (http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) –
Также 'public int ChildID { получить; задавать; } 'следует удалить (у вас есть коллекция' Child', а не один элемент) –