Guest User

Untitled

a guest
Dec 11th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. public async Task<Unit> Handle(CreateCommand request, CancellationToken cancellationToken)
  2. {
  3. var entity = new Customer
  4. {
  5. CompanyName = request.CompanyName,
  6. Country = request.Country,
  7. City = request.City,
  8. Address = request.Address,
  9. ZipCode = request.ZipCode,
  10. };
  11. _context.Customers.Add(entity);
  12.  
  13. await _context.SaveChangesAsync(cancellationToken);
  14.  
  15. foreach (var createCustEmail in request.Emails)
  16. {
  17. _context.CustomerEmails.Add(new CustomerEmail()
  18. {
  19. CustomerId = entity.CustomerId,
  20. Email = createCustEmail.Email,
  21. });
  22. }
  23. await _context.SaveChangesAsync(cancellationToken);
  24.  
  25. return Unit.Value;
  26. }
  27.  
  28. public class Customer
  29. {
  30. public int Id { get; set; }
  31.  
  32. public string Title { get; set; }
  33.  
  34. public string[] Phones { get; set; }
  35. }
  36.  
  37. public class Contact
  38. {
  39. public Contact()
  40. {
  41. this.Phones = new List<Phone>();
  42. }
  43.  
  44. public int Id { get; set; }
  45.  
  46. public string FirstName { get; set; }
  47.  
  48. public string LastName { get; set; }
  49.  
  50. public virtual ICollection<Phone> Phones { get; set; }
  51. }
  52.  
  53. public class Phone
  54. {
  55. public int Id { get; set; }
  56.  
  57. public int ContactId { get; set; }
  58.  
  59. public virtual Contact Contact { get; set; }
  60.  
  61. public string Number { get; set; }
  62. }
  63.  
  64. <div id="phoneNumbers">
  65. @Html.EditorFor(x => x.Phones)
  66. </div>
  67.  
  68. @model WebApplication1.Models.Contact
  69.  
  70. @{
  71. ViewBag.Title = "Create";
  72. }
  73.  
  74. <h2>Create</h2>
  75.  
  76.  
  77. @using (Html.BeginForm())
  78. {
  79. @Html.AntiForgeryToken()
  80.  
  81. <div class="form-horizontal">
  82. <h4>Contact</h4>
  83. <hr />
  84. @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  85. <div class="form-group">
  86. @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
  87. <div class="col-md-10">
  88. @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
  89. @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
  90. </div>
  91. </div>
  92.  
  93. <div class="form-group">
  94. @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
  95. <div class="col-md-10">
  96. @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
  97. @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
  98. </div>
  99. </div>
  100.  
  101. <div id="phoneNumbers">
  102. @Html.EditorFor(x => x.Phones)
  103. </div>
  104.  
  105. <div class="form-group">
  106. <div class="col-md-offset-2 col-md-10">
  107. <input type="submit" value="Create" class="btn btn-default" />
  108. </div>
  109. </div>
  110. </div>
  111. }
  112.  
  113. <div>
  114. @Html.ActionLink("Back to List", "Index")
  115. </div>
  116.  
  117. @section Scripts {
  118. @Scripts.Render("~/bundles/jqueryval")
  119. }
  120.  
  121. @model WebApplication1.Models.Phone
  122.  
  123. <div class="phoneRow">
  124. @Html.HiddenFor(x => x.Id)
  125. @Html.HiddenFor(x => x.ContactId)
  126. <p>
  127. <label>Phone Number</label>
  128. @Html.TextBoxFor(x => x.Number)
  129. </p>
  130. <a href="javascript:void(0);" class="remRowPhone"><span class="glyphicon glyphicon glyphicon-trash"></span>Удалить</a>
  131. </div>
  132.  
  133. // POST: Contacts/Edit/5
  134. [HttpPost]
  135. [ValidateAntiForgeryToken]
  136. public ActionResult Edit(Contact contact)
  137. {
  138. if (!this.ModelState.IsValid)
  139. return this.View(contact);
  140.  
  141. var entity = db.Contacts.Include(x=>x.Phones).SingleOrDefault(x => x.Id == contact.Id);
  142. if (entity == null)
  143. return this.HttpNotFound();
  144.  
  145. this.db.UpdateGraph(contact, map => map.OwnedCollection(p => p.Phones));
  146.  
  147. this.db.SaveChanges();
  148. return this.RedirectToAction("Index");
  149. }
Add Comment
Please, Sign In to add comment