Есть 2 модели, связанные связью многие-ко-многим. Мой вопрос: как можно реализовать добавление? какой код нужно написать для контроллера и для view? Я хочу, чтобы пользователь мог сам вводить данные как о себе, так и о машине. Что мне нужно дописать, чтобы можно было добавить информацию о машине? Вот мой код:
public class Owner
{
public int OwnerID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public DateTime Birthday { get; set; }
public int Experience { get; set; }
public virtual ICollection<Car> Cars { get; set; }
}
public class Car
{
public int CarID { get; set; }
public string Model { get; set; }
public string Mark { get; set; }
public string Type { get; set; }
public int Price { get; set; }
public string YearofRelease { get; set; }
public virtual ICollection<Owner> Owners { get; set; }
}
Контроллер:
// GET: Owners/Create
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "OwnerID,Name,Surname,Birthday,Experience")] Owner owner, [Bind(Include = "CarID,Model,Mark,Type,Price,YearOfRelease")] Car car )
{
if (ModelState.IsValid)
{
owner.Cars.Add(car);
db.Owners.Add(owner);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(owner);
}
Отображение
@model Site.Models.Owner
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Owner</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>
<div class="form-group">
@Html.LabelFor(model => model.Birthday, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Birthday, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Birthday, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Experience, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Experience, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Experience, "", 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>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}