/// <summary> /// GET: /Address/Create /// </summary> /// <param name="id">Company id</param> /// <param name="contactId"></param> /// <returns></returns> public ActionResult Create(int id, int contactId) { AddressVM addr = new AddressVM(); addr.CompanyId = id; addr.ContactId = contactId; addr.AddressTypeList = db.AddressTypes; return View(addr); }
/// <summary> /// Gets list of addresses associated with a specific contact. /// </summary> /// <param name="contactId"></param> /// <returns></returns> private ICollection<AddressVM> GetAddresses(int contactId) { AddressVM vm = new AddressVM(); List<Address> addresses = db.Addresses.Where(c => c.Contact.Id == contactId).ToList(); ICollection<AddressVM> addressList = new List<AddressVM>(); foreach (Address address in addresses) { addressList.Add(vm.addressToViewModel(address)); } return addressList; }
public AddressVM addressToViewModel(Address address) { AddressVM model = new AddressVM { Id = address.Id, CompanyId = address.Contact.Company.Id, ContactId = address.Contact.Id, AddressType = address.AddressType, AddressLine1 = address.AddressLine1, AddressLine2 = address.AddressLine2, City = address.City, State = address.State, ZipCode = address.ZipCode, DateCreated = address.DateCreated, DateModified = address.DateModified }; return model; }
public Address viewModelToAddress(AddressVM model) { Address address = new Address { Id = model.Id, AddressType = model.AddressType, AddressLine1 = model.AddressLine1, AddressLine2 = model.AddressLine2, City = model.City, State = model.State, ZipCode = model.ZipCode, DateCreated = model.DateCreated, DateModified = model.DateModified }; return address; }
/// <summary> /// GET: /Address/Edit/ /// </summary> /// <param name="id">Company id.</param> /// <param name="contactId"></param> /// <param name="addrId"></param> /// <returns></returns> public ActionResult Edit(int id, int contactId, int addrId) { AddressVM model = new AddressVM(); Address address = db.Addresses.Find(addrId); model = model.addressToViewModel(address); model.AddressTypeList = db.AddressTypes; model.CompanyId = id; model.ContactId = contactId; if (address == null) { return HttpNotFound(); } ModelState.Clear(); return View(model); }