Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. public class Parent
  2. {
  3.  
  4. public int ParentID { get; set; }
  5.  
  6. public string FirstName { get; set; }
  7. public string LastName { get; set; }
  8.  
  9. public virtual Court Court { get; set; }
  10. //public IEnumerable<SelectListItem> Courts { get; set; }
  11. public virtual ICollection<Child> Childs { get; set; }
  12.  
  13. }
  14.  
  15. public class Court
  16. {
  17. public int CourtId { get; set; }
  18. public string CourtName { get; set; }
  19.  
  20. public virtual ICollection<Parent> Parents { get; set; }
  21. }
  22.  
  23. public class ParentVM
  24. {
  25.  
  26. public int ParentID { get; set; }
  27.  
  28. public string FirstName { get; set; }
  29. public string LastName { get; set; }
  30.  
  31. //public int CourtId { get; set; }
  32. //public string CourtName { get; set; }
  33.  
  34. public virtual Court Court { get; set; }
  35. //public virtual IEnumerable<Court> CourtList { get; set; }
  36. public IEnumerable<SelectListItem> Courts { get; set; }
  37.  
  38. public IList<ChildVM> Children { get; set; }
  39.  
  40. }
  41.  
  42. // GET: Parents/Create
  43. public ActionResult Create()
  44. {
  45.  
  46. IEnumerable<SelectListItem> CourtList = db.Courts.ToList().Select(x => new SelectListItem
  47. {
  48. Value = x.CourtId.ToString(),
  49. Text = x.CourtName,
  50.  
  51. });
  52.  
  53.  
  54.  
  55. //ViewBag.CourtList = new SelectList(db.Courts, "CourtId", "CourtName");
  56.  
  57. ParentVM viewModel = new ParentVM()
  58. {
  59. Children = new List<ChildVM>()
  60. {
  61. new ChildVM(){Name="", DOB="", Address=""},
  62. //new ChildVM(){Name="2", DOB="2", Address="222"},
  63. //new ChildVM(){Name="3", DOB="3", Address="3"},
  64. },
  65.  
  66. viewModel.Courts = CourtList,
  67. };
  68.  
  69. return View(viewModel);
  70.  
  71. }
  72.  
  73. // POST: Parents/Create
  74. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  75. // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
  76. [HttpPost]
  77. [ValidateAntiForgeryToken]
  78. public ActionResult Create(ParentVM viewModel)
  79. {
  80. if (ModelState.IsValid)
  81. {
  82.  
  83. var parent = new Parent()
  84. {
  85. FirstName = viewModel.FirstName,
  86. LastName = viewModel.LastName
  87. };
  88.  
  89. db.Parents.Add(parent);
  90.  
  91. foreach (ChildVM item in viewModel.Children)
  92. {
  93.  
  94. var child = new Child()
  95. {
  96. Name = item.Name,
  97. DOB = item.DOB,
  98. Address = item.Address
  99. };
  100.  
  101. db.Childs.Add(child);
  102. }
  103.  
  104. //Parent parent = new Parent();
  105. //var employee = AutoMapper.Mapper.Map<Parent, ParentVM>(parent);
  106.  
  107. db.SaveChanges();
  108. return RedirectToAction("Index");
  109. }
  110.  
  111. return View(viewModel);
  112. }
  113.  
  114. @Html.DropDownList("Courts", (IEnumerable<SelectListItem>)Model.Courts)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement