// GET: Contacts/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var contact = await _bll.Contacts.FindForUserAsync(id.Value, User.GetUserId()); if (contact == null) { return(NotFound()); } var vm = new ContactCreateEditViewModel(); vm.Contact = contact; vm.ContactTypeSelectList = new SelectList( await _bll.ContactTypes.AllAsync(), nameof(BLL.App.DTO.ContactType.Id), nameof(BLL.App.DTO.ContactType.ContactTypeValue), vm.Contact.ContactTypeId); vm.PersonSelectList = new SelectList( await _bll.Persons.AllForUserAsync(User.GetUserId()), nameof(BLL.App.DTO.Person.Id), nameof(BLL.App.DTO.Person.FirstLastName), vm.Contact.PersonId); return(View(vm)); }
public async Task <IActionResult> Edit(int id, ContactCreateEditViewModel vm) { if (id != vm.Contact.Id) { return(NotFound()); } if (!await _bll.Contacts.BelongsToUserAsync(id, User.GetUserId())) { return(NotFound()); } if (ModelState.IsValid) { _bll.Contacts.Update(vm.Contact); await _bll.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } vm.ContactTypeSelectList = new SelectList( await _bll.ContactTypes.AllAsync(), nameof(BLL.App.DTO.ContactType.Id), nameof(BLL.App.DTO.ContactType.ContactTypeValue), vm.Contact.ContactTypeId); vm.PersonSelectList = new SelectList( await _bll.Persons.AllForUserAsync(User.GetUserId()), nameof(BLL.App.DTO.Person.Id), nameof(BLL.App.DTO.Person.FirstLastName), vm.Contact.PersonId); return(View(vm)); }
// GET: Contacts/Create public ActionResult Create() { // ReSharper disable once UseObjectOrCollectionInitializer var vm = new ContactCreateEditViewModel(); vm.ContactTypeSelectList = new SelectList(_uow.ContactTypes.All.Select(t => new { t.ContactTypeId, ContactTypeName = t.ContactTypeName.Translate() }).ToList(), nameof(ContactType.ContactTypeId), nameof(ContactType.ContactTypeName)); vm.PersonSelectList = new SelectList(_uow.Persons.GetAllForUser(User.Identity.GetUserId <int>()), nameof(Person.PersonId), nameof(Person.FirstLastname)); return(View(vm)); }
public ActionResult Edit(ContactCreateEditViewModel vm) { if (ModelState.IsValid) { _uow.Contacts.Update(vm.Contact); _uow.Commit(); return(RedirectToAction(nameof(Index))); } vm.ContactTypeSelectList = new SelectList(_uow.ContactTypes.All.Select(t => new { t.ContactTypeId, ContactTypeName = t.ContactTypeName.Translate() }).ToList(), nameof(ContactType.ContactTypeId), nameof(ContactType.ContactTypeName), vm.Contact.ContactTypeId); vm.PersonSelectList = new SelectList(_uow.Persons.GetAllForUser(User.Identity.GetUserId <int>()), nameof(Person.PersonId), nameof(Person.FirstLastname), vm.Contact.PersonId); return(View(vm)); }
// GET: Contacts/Create public async Task <IActionResult> Create() { var vm = new ContactCreateEditViewModel() { ContactTypeSelectList = new SelectList( await _bll.ContactTypes.AllAsync(), nameof(BLL.App.DTO.ContactType.Id), nameof(BLL.App.DTO.ContactType.ContactTypeValue)), PersonSelectList = new SelectList( await _bll.Persons.AllForUserAsync(User.GetUserId()), nameof(BLL.App.DTO.Person.Id), nameof(BLL.App.DTO.Person.FirstLastName)) }; return(View(vm)); }
public ActionResult Delete(int?id) { if (id != null) { ContactRepository contactRepo = new ContactRepository(); Contact contact = contactRepo.GetById(id.Value); if (contact != null && contact.UserID == AuthenticationService.LoggedUser.Id) { ContactCreateEditViewModel model = new ContactCreateEditViewModel() { Id = contact.Id, FirstName = contact.FirstName, LastName = contact.LastName, Email = contact.Email }; GroupRepository groupRepo = new GroupRepository(); List <Group> contactGroups = contactRepo.GetAll().Where(c => c.Id == id).First().Groups; if (contactGroups.Count > 0) { List <CheckBoxListItem> checkBoxListItems = new List <CheckBoxListItem>(); foreach (Group group in contactGroups) { checkBoxListItems.Add(new CheckBoxListItem() { Text = group.Name }); } model.Groups = checkBoxListItems; } return(View(model)); } } return(RedirectToAction("Index")); }
// GET: Contacts/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var contact = _uow.Contacts.GetForUser(id.Value, User.Identity.GetUserId <int>()); if (contact == null) { return(HttpNotFound()); } // ReSharper disable once UseObjectOrCollectionInitializer var vm = new ContactCreateEditViewModel(); vm.Contact = contact; vm.ContactTypeSelectList = new SelectList(_uow.ContactTypes.All.Select(t => new { t.ContactTypeId, ContactTypeName = t.ContactTypeName.Translate() }).ToList(), nameof(ContactType.ContactTypeId), nameof(ContactType.ContactTypeName), vm.Contact.ContactTypeId); vm.PersonSelectList = new SelectList(_uow.Persons.GetAllForUser(User.Identity.GetUserId <int>()), nameof(Person.PersonId), nameof(Person.FirstLastname), vm.Contact.PersonId); return(View(vm)); }
public ActionResult CreateEdit(ContactCreateEditViewModel model) { if (!ModelState.IsValid) { return(View(model)); } Contact contact; ContactRepository contactRepo = new ContactRepository(); if (model.Id > 0) // Edit { contact = contactRepo.GetById(model.Id); if (contact == null || contact.UserID != model.UserID) { return(HttpNotFound()); } } else // Create { contact = new Contact() { UserID = model.UserID, Groups = new List <Group>() }; } if (contact.UserID == AuthenticationService.LoggedUser.Id) { if (model.GroupIds != null) { GroupRepository groupRepo = new GroupRepository(); groupRepo.Context = contactRepo.Context; groupRepo.DbSet = groupRepo.Context.Set <Group>(); if (contact.Groups != null) { contact.Groups.Clear(); } foreach (int groupId in model.GroupIds) { Group group = groupRepo.GetById(groupId); contact.Groups.Add(group); } } else { contact.Groups.Clear(); } contact.FirstName = model.FirstName; contact.LastName = model.LastName; contact.Email = model.Email; contactRepo.Save(contact); return(RedirectToAction("Index")); } return(HttpNotFound()); }
public ActionResult CreateEdit(int?id) { if (id == null) // Create { ContactCreateEditViewModel model = new ContactCreateEditViewModel() { UserID = AuthenticationService.LoggedUser.Id }; GroupRepository groupRepo = new GroupRepository(); List <Group> allGroups = groupRepo.GetAll(g => g.UserID == AuthenticationService.LoggedUser.Id); List <CheckBoxListItem> checkBoxListItems = new List <CheckBoxListItem>(); foreach (Group group in allGroups) { checkBoxListItems.Add(new CheckBoxListItem() { Id = group.Id, Text = group.Name, IsChecked = false }); } model.Groups = checkBoxListItems; return(View(model)); } if (id > 0) // Edit { ContactRepository contactRepo = new ContactRepository(); Contact contact = contactRepo.GetById(id.Value); if (contact != null && contact.UserID == AuthenticationService.LoggedUser.Id) { ContactCreateEditViewModel model = new ContactCreateEditViewModel() { Id = contact.Id, UserID = contact.UserID, FirstName = contact.FirstName, LastName = contact.LastName, Email = contact.Email }; GroupRepository groupRepo = new GroupRepository(); List <Group> allGroups = groupRepo.GetAll(g => g.UserID == AuthenticationService.LoggedUser.Id); //List<Group> contactGroups = groupRepo.GetAll(g => g.Contacts.Contains(contact)); List <Group> contactGroups = contactRepo.GetAll().Where(c => c.Id == id).First().Groups; List <CheckBoxListItem> checkBoxListItems = new List <CheckBoxListItem>(); foreach (Group group in allGroups) { checkBoxListItems.Add(new CheckBoxListItem() { Id = group.Id, Text = group.Name, IsChecked = contactGroups.Where(g => g.Id == group.Id).Any() }); } model.Groups = checkBoxListItems; return(View(model)); } } return(RedirectToAction("Index")); }
public ContactCreateEditPage(ContactViewModel contact, List <Category> categories) { InitializeComponent(); ViewModel = new ContactCreateEditViewModel(contact, categories); }