Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public async Task<Unit> Handle(CreateCommand request, CancellationToken cancellationToken)
- {
- var entity = new Customer
- {
- CompanyName = request.CompanyName,
- Country = request.Country,
- City = request.City,
- Address = request.Address,
- ZipCode = request.ZipCode,
- };
- _context.Customers.Add(entity);
- await _context.SaveChangesAsync(cancellationToken);
- foreach (var createCustEmail in request.Emails)
- {
- _context.CustomerEmails.Add(new CustomerEmail()
- {
- CustomerId = entity.CustomerId,
- Email = createCustEmail.Email,
- });
- }
- await _context.SaveChangesAsync(cancellationToken);
- return Unit.Value;
- }
- public class Customer
- {
- public int Id { get; set; }
- public string Title { get; set; }
- public string[] Phones { get; set; }
- }
- public class Contact
- {
- public Contact()
- {
- this.Phones = new List<Phone>();
- }
- public int Id { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public virtual ICollection<Phone> Phones { get; set; }
- }
- public class Phone
- {
- public int Id { get; set; }
- public int ContactId { get; set; }
- public virtual Contact Contact { get; set; }
- public string Number { get; set; }
- }
- <div id="phoneNumbers">
- @Html.EditorFor(x => x.Phones)
- </div>
- @model WebApplication1.Models.Contact
- @{
- ViewBag.Title = "Create";
- }
- <h2>Create</h2>
- @using (Html.BeginForm())
- {
- @Html.AntiForgeryToken()
- <div class="form-horizontal">
- <h4>Contact</h4>
- <hr />
- @Html.ValidationSummary(true, "", new { @class = "text-danger" })
- <div class="form-group">
- @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
- </div>
- </div>
- <div id="phoneNumbers">
- @Html.EditorFor(x => x.Phones)
- </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")
- }
- @model WebApplication1.Models.Phone
- <div class="phoneRow">
- @Html.HiddenFor(x => x.Id)
- @Html.HiddenFor(x => x.ContactId)
- <p>
- <label>Phone Number</label>
- @Html.TextBoxFor(x => x.Number)
- </p>
- <a href="javascript:void(0);" class="remRowPhone"><span class="glyphicon glyphicon glyphicon-trash"></span>Удалить</a>
- </div>
- // POST: Contacts/Edit/5
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Edit(Contact contact)
- {
- if (!this.ModelState.IsValid)
- return this.View(contact);
- var entity = db.Contacts.Include(x=>x.Phones).SingleOrDefault(x => x.Id == contact.Id);
- if (entity == null)
- return this.HttpNotFound();
- this.db.UpdateGraph(contact, map => map.OwnedCollection(p => p.Phones));
- this.db.SaveChanges();
- return this.RedirectToAction("Index");
- }
Add Comment
Please, Sign In to add comment