Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Insert/update multiple objects
- public class Location
- {
- public int Id { get; set; }
- public string Description { get; set; }
- public string CarId { get; set; }
- public Car Car { get; set; }
- }
- public class Car
- {
- public string Id { get; set; }
- public string Color { get; set; }
- }
- <div>@Html.LabelFor(location => location.Description)</div>
- <div>@Html.EditorFor(location => location.Description)</div>
- <div>@Html.LabelFor(location => location.Car.Id)</div>
- <div>@Html.EditorFor(location => location.Car.Id)</div>
- <div>@Html.LabelFor(location => location.Car.Color)</div>
- <div>@Html.EditorFor(location => location.Car.Color)</div>
- [HttpPost]
- public ActionResult Create(Location location)
- {
- if (ModelState.IsValid)
- {
- Car car = db.Car.Find(location.Car.Id);
- if (car != null)
- db.Entry(car).CurrentValues.SetValues(location.Car);
- else
- db.Car.Add(location.Car);
- db.Location.Add(locacao);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- return View(locacao);
- }
- db.Location.Add(locacation);
- // You can also use another way to find if you are working with a new Car
- // like location.Car.Id == 0
- if (db.Car.Any(c => c.Id == location.Car.Id))
- {
- db.Entry(location.Car).State = EntityState.Modified;
- }
- db.SaveChanges();
Add Comment
Please, Sign In to add comment