There are 2 models connected by many-to-many communication. My question is: how can I implement the add? What code do I need to write for the controller and for the view? I want the user to be able to enter data both about himself and about the car. What do I need to add to be able to add information about the car? Here is my code:
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; } } Controller:
// 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); } Display
@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") }