public ActionResult AddContact(int id /*id of the account*/, FormCollection formValues) { int contactId = Int32.Parse(formValues["ContactId"]); //we retrieve the contact to add to the contact Contact contact = _contactService.GetContact(contactId); //we retrieve the account to add the contact to Account account = _accountService.GetAccount(id); account.Contacts.Add(contact); if (!_accountService.EditAccount(account)) { //we redisplay the form in case something goes wrong string userName = this.User.Identity.Name; //we retrieve from the db the contacts of the user, that haven't been assigned to an account yet var contacts = _contactService.ListContactsByCriteria(c => (c.ResponsibleUser.UserName == userName) && (c.Account == null)); var viewModel = new AddContactViewModel { AccountId = id, Contacts = new SelectList(contacts, "Id", "Email", 1) }; return View(viewModel); } return View("Details", account); }
// // GET: /Account/AddTask public ActionResult AddContact(int id /*id of the account*/) { string userName = this.User.Identity.Name; //we retrieve from the db the contacts of the user, that haven't been assigned to an account yet var tasks = _contactService.ListContactsByCriteria(contact => (contact.ResponsibleUser.UserName == userName) && (contact.Account == null)); var viewModel = new AddContactViewModel { AccountId = id, Contacts = new SelectList(tasks, "Id", "Email", 1) }; return View(viewModel); }