public ActionResult Edit(int id, CompanyCreateEditViewModel editedCompany) { // Pobranie firmy po identyfikatorze var company = _companyRepo.GetCompanyById(id); editedCompany.Company = company; // Uaktualnienie modelu i zapisanie zmian w bazie danych if (TryUpdateModel(editedCompany)) { editedCompany.Company.WebPage = ChangeUrlAddress(editedCompany.Company.WebPage); editedCompany.Company.AbsolventWebPage = ChangeUrlAddress(editedCompany.Company.AbsolventWebPage); editedCompany.Company.GoldenLineWebPage = ChangeUrlAddress(editedCompany.Company.GoldenLineWebPage); editedCompany.Company.GoworkWebPage = ChangeUrlAddress(editedCompany.Company.GoworkWebPage); _companyRepo.SaveChanges(); TempData["Message"] = "Pomyślnie zmodyfikowano wpis!"; return(RedirectToAction("Details", new { id = editedCompany.Company.Id })); } else { // Pobranie listy kategorii firm i umieszczenie jej w obiekcie ViewData ViewData["Categories"] = GetEditCategories(editedCompany.Company); TempData["Error"] = "Wystąpił błąd podczas edytowania wpisu!"; return(View(editedCompany)); } }
public ActionResult Create(CompanyCreateEditViewModel company) { if (ModelState.IsValid) { // Dodanie nowej firmy i zapisanie w bazie danych if (TryUpdateModel(company)) { // Ustawienie daty umieszczenia firmy company.Company.PostedDate = DateTime.Now; // Ustawienie użytkownika dodającego firmę oraz adresu IP company.Company.UserId = WebSecurity.CurrentUserId; company.Company.IPAddress = Request.UserHostAddress; company.Company.WebPage = ChangeUrlAddress(company.Company.WebPage); company.Company.AbsolventWebPage = ChangeUrlAddress(company.Company.AbsolventWebPage); company.Company.GoldenLineWebPage = ChangeUrlAddress(company.Company.GoldenLineWebPage); company.Company.GoworkWebPage = ChangeUrlAddress(company.Company.GoworkWebPage); // Dodanie nowej firmy i zapisanie zmian _companyRepo.Add(company.Company); _companyRepo.SaveChanges(); return(RedirectToAction("Details", new { id = company.Company.Id })); } // Pobranie listy kategorii firm i umieszczenie jej w obiekcie ViewData ViewData["Categories"] = GetCategoriesList(); return(View(company)); } TempData["Error"] = "Wystąpił błąd podczas dodawania wpisu!"; return(RedirectToAction("Index")); }
public async Task <IActionResult> Create(CompanyCreateEditViewModel vm) { vm.AppUserId = User.UserGuidId(); if (ModelState.IsValid) { var bllEntity = _mapper.Map(vm); _bll.Companies.Add(bllEntity); await _bll.SaveChangesAsync(); vm.Id = bllEntity.Id; var appUserCompanyBLL = new AppUserCompanyBLL() { AppUserId = User.UserGuidId(), CompanyId = bllEntity.Id }; _bll.AppUserCompanies.Add(appUserCompanyBLL); await _bll.SaveChangesAsync(); return(RedirectToAction(nameof(AppUserCompanies))); } vm.LocationSelectList = new SelectList(await _bll.Locations.GetAllAsync(User.UserGuidId()), nameof(LocationBLL.Id), nameof(LocationBLL.AddressLine), vm.LocationId); return(View(vm)); }
public async Task <IActionResult> Edit(Guid id, CompanyCreateEditViewModel vm) { if (id != vm.Id) { return(BadRequest(new MessageDTO("Id and vm.id do not match"))); } if (!await _bll.Companies.ExistsAsync(vm.Id, User.UserGuidId())) { return(NotFound(new MessageDTO($"Current user does not have company with this id {id}"))); } vm.AppUserId = User.UserGuidId(); if (ModelState.IsValid) { await _bll.Companies.UpdateAsync(_mapper.Map(vm)); await _bll.SaveChangesAsync(); return(RedirectToAction(nameof(AppUserCompanies))); } vm.LocationSelectList = new SelectList(await _bll.Locations.GetAllAsync(User.UserGuidId()), nameof(LocationBLL.Id), nameof(LocationBLL.AddressLine), vm.LocationId); return(View(vm)); }
// GET: Companies/Create public async Task <IActionResult> Create() { var vm = new CompanyCreateEditViewModel { LocationSelectList = new SelectList(await _bll.Locations.GetAllAsync(User.UserGuidId()), nameof(LocationBLL.Id), nameof(LocationBLL.AddressLine)) }; return(View(vm)); }
public ActionResult Edit(int id = 0) { // Pobranie firmy po identyfikatorze var company = _companyRepo.GetCompanyById(id); if (company != null) { // Pobranie listy kategorii firm i umieszczenie jej w obiekcie ViewData ViewData["Categories"] = GetEditCategories(company); // Utworzenie modelu widokowego w celu przekazania go do widoku CompanyCreateEditViewModel editCompany = new CompanyCreateEditViewModel(); editCompany.Company = company; // Sprawdzenie, czy zalogowany użytkownik może edytować usługę if (Roles.GetRolesForUser(WebSecurity.CurrentUserName).Contains("ServiceProvider") || Roles.GetRolesForUser(WebSecurity.CurrentUserName).Contains("Administrator")) { if (company.UserId == WebSecurity.CurrentUserId) { return(View(editCompany)); } else if (Roles.GetRolesForUser(WebSecurity.CurrentUserName).Contains("Administrator")) { return(View(editCompany)); } else { TempData["Error"] = "Nie masz uprawnień do edytowania tego wpisu."; return(RedirectToAction("Index")); } } else { TempData["Error"] = "Nie masz uprawnień do edytowania tego wpisu."; return(RedirectToAction("Index")); } } else { TempData["Error"] = "Brak wpisu o podanym ID!"; return(RedirectToAction("Index")); } }