public ActionResult New() { var personRoles = _db.PersonRoles.ToList(); if (!personRoles.Any()) { TempData["error"] = string.Format( @"No roles have been created. <a href=""{0}"" class=""alert-link"" > Click here </a> to add a role now.", Url.Action("New", "PersonRole")); return(RedirectToAction("Index")); } var sites = GetAvailableSitesForUser(); if (sites == null) { TempData["error"] = "You were not found in the 'Person' database. Please contact admin"; return(RedirectToAction("Index")); } if (!sites.Any()) { //if the user is an admin, it means there's probably not been any added TempData["error"] = string.Format( @"No sites have been created. <a href=""{0}"" class=""alert-link"" > Click here </a> to add a site now.", Url.Action("New", "Site")); return(RedirectToAction("Index")); } var personViewModel = new PersonAddEditViewModel { AvailablePersonRoles = personRoles, AvailableSites = sites, Person = new Person() }; //if we're a site admin, set the person site id to our site if (User.IsInRole("Site Admin")) { personViewModel.Person.SiteId = sites.First().Id; } return(View("NewOrEdit", personViewModel)); }
public async Task <ActionResult> Edit(int id) { var personRoles = _db.PersonRoles.ToList(); var person = await _db.People. SingleOrDefaultAsync(x => x.Id.Equals(id)); if (person == null) { return(new HttpNotFoundResult()); } var personViewModel = new PersonAddEditViewModel { AvailablePersonRoles = personRoles, AvailableSites = GetAvailableSitesForUser(), Person = person }; return(View("NewOrEdit", personViewModel)); }