Reputation: 316
I have the following code and want to update the user role with dropdown then stay on this page with the updated choice from the dropdown. I thought I could use redirectToAction to the GET ActonResult but it goes to the POST ActionResult. How do I stay on the same view with the dropdown updated with new choice? Thanks
[HttpGet]
public ActionResult Details(Guid? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Employee employee = db.Employees.Find(id);
EmployeeViewModel empv = new EmployeeViewModel();
empv.FirstName = employee.FirstName;
empv.LastName = employee.LastName;
empv.NetworkLoginID = employee.NetworkLoginID;
empv.PersonID = employee.PersonID;
empv.DataStatusID = employee.DataStatusID;
empv.EmployeeGUID = employee.EmployeeGUID;
Guid UserRoleGuid = (from e in db.EmployeeToUserRoles
where e.EmployeeGUID == empv.EmployeeGUID
select e.RoleGUID).FirstOrDefault();
empv.RoleID = UserRoleGuid;
if (employee == null)
{
return HttpNotFound();
}
PopulatedDropDown(empv.RoleID);
return View(empv);
}
[HttpPost]
public ActionResult Details(Guid EmployeeGUID, Guid Roles)
{
if (ModelState.IsValid)
{
EmployeeToUserRole emp = new EmployeeToUserRole();
//check if employee was in role and update column
var inrole = from d in db.EmployeeToUserRoles
where d.EmployeeGUID == EmployeeGUID
select d.EmployeeRoleGUID;
int count = inrole.Count();
if (count > 0 )
{
emp = db.EmployeeToUserRoles.Where(s => s.EmployeeGUID == EmployeeGUID).FirstOrDefault<EmployeeToUserRole>();
emp.RoleGUID = Roles;
db.Entry(emp).State = EntityState.Modified;
db.SaveChanges();
}
else
{
emp.EmployeeRoleGUID = Guid.NewGuid();
emp.RoleGUID = Roles;
emp.EmployeeGUID = EmployeeGUID;
db.EmployeeToUserRoles.Add(emp);
db.SaveChanges();
}
PopulatedDropDown(Roles);
RedirectToAction("Details", new { id = emp.EmployeeGUID });
}
return View();
}
Upvotes: 0
Views: 248
Reputation: 218882
You need to return the RedirectToAction Result.
return RedirectToAction("Details", new { id = emp.EmployeeGUID });
This will make the browser to do a GET request to the Details action with the value of emp.EmployeeGUID
. You need to set the corresponding property (for the role) of your view model in that after reading the Employee entity from your db.
Upvotes: 1