Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Parent
- {
- public int ParentID { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public virtual Court Court { get; set; }
- //public IEnumerable<SelectListItem> Courts { get; set; }
- public virtual ICollection<Child> Childs { get; set; }
- }
- public class Court
- {
- public int CourtId { get; set; }
- public string CourtName { get; set; }
- public virtual ICollection<Parent> Parents { get; set; }
- }
- public class ParentVM
- {
- public int ParentID { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- //public int CourtId { get; set; }
- //public string CourtName { get; set; }
- public virtual Court Court { get; set; }
- //public virtual IEnumerable<Court> CourtList { get; set; }
- public IEnumerable<SelectListItem> Courts { get; set; }
- public IList<ChildVM> Children { get; set; }
- }
- // GET: Parents/Create
- public ActionResult Create()
- {
- IEnumerable<SelectListItem> CourtList = db.Courts.ToList().Select(x => new SelectListItem
- {
- Value = x.CourtId.ToString(),
- Text = x.CourtName,
- });
- //ViewBag.CourtList = new SelectList(db.Courts, "CourtId", "CourtName");
- ParentVM viewModel = new ParentVM()
- {
- Children = new List<ChildVM>()
- {
- new ChildVM(){Name="", DOB="", Address=""},
- //new ChildVM(){Name="2", DOB="2", Address="222"},
- //new ChildVM(){Name="3", DOB="3", Address="3"},
- },
- viewModel.Courts = CourtList,
- };
- return View(viewModel);
- }
- // POST: Parents/Create
- // To protect from overposting attacks, please enable the specific properties you want to bind to, for
- // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Create(ParentVM viewModel)
- {
- if (ModelState.IsValid)
- {
- var parent = new Parent()
- {
- FirstName = viewModel.FirstName,
- LastName = viewModel.LastName
- };
- db.Parents.Add(parent);
- foreach (ChildVM item in viewModel.Children)
- {
- var child = new Child()
- {
- Name = item.Name,
- DOB = item.DOB,
- Address = item.Address
- };
- db.Childs.Add(child);
- }
- //Parent parent = new Parent();
- //var employee = AutoMapper.Mapper.Map<Parent, ParentVM>(parent);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- return View(viewModel);
- }
- @Html.DropDownList("Courts", (IEnumerable<SelectListItem>)Model.Courts)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement