我有以下代码,并希望使用下拉列表更新用户角色,然后使用下拉列表中的更新选项保留此页面。我以为我可以将redirectToAction用于GET ActonResult,但它会转到POST ActionResult。如何使用新选项更新下拉列表以保持相同的视图?感谢
[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();
}
答案 0 :(得分:1)
您需要返回RedirectToAction结果。
return RedirectToAction("Details", new { id = emp.EmployeeGUID });
这将使浏览器对“详细信息”操作执行GET请求,其值为emp.EmployeeGUID
。在从数据库中读取Employee实体后,您需要在其中设置视图模型的相应属性(用于角色)。