C# 如何在MVC5中将一个操作重定向到另一个操作?
我想C# 如何在MVC5中将一个操作重定向到另一个操作?,c#,.net,asp.net-mvc-5,C#,.net,Asp.net Mvc 5,我想重定向到方法内部的另一个操作。我用过: return RedirectToAction("ActionName", "ControllerName"); 但是我得到一个错误,它没有击中其他动作 我的控制器代码: public ActionResult VisitCount(VisitorsViewModel objvvm) { var UserID = System.Web.HttpContext.Current.Session["UserID"].ToString();
重定向到方法内部的另一个操作。我用过:
return RedirectToAction("ActionName", "ControllerName");
但是我得到一个错误,它没有击中其他动作
我的控制器代码:
public ActionResult VisitCount(VisitorsViewModel objvvm)
{
var UserID = System.Web.HttpContext.Current.Session["UserID"].ToString();
var objEmpDepUTID = db.UserRightsSettings
.Where(u => u.UserID.ToString() == UserID)
.Select(e => new
{
objemployeeID = e.EmployeeID,
objdepartmentID = e.DepartmentID,
objusertypeID = e.UserTypeID
})
.FirstOrDefault();
var EmployeeID = objEmpDepUTID.objemployeeID;
var DepartmentID = objEmpDepUTID.objdepartmentID;
var UserTypeID = objEmpDepUTID.objusertypeID;
if (DepartmentID == new Guid("47D2C992-1CB6-44AA-91CA-6AA3C338447E") &&
(UserTypeID == new Guid("106D02CC-7DC2-42BF-AC6F-D683ADDC1824") ||
(UserTypeID == new Guid("B3728982-0016-4562-BF73-E9B8B99BD501"))))
{
return RedirectToAction("Index1", "NextFollowUp");
}
else
{
return RedirectToAction("Index2", "NextFollowUp");
}
}
private ActionResult Index1()
{
return View();
}
private ActionResult Index2()
{
return View();
}
上面的代码工作不正常-当if条件满足时,它不会命中其他操作。我尝试了所有方法,但没有找到任何解决方案。如果您在同一个控制器中,请尝试以下方法:
return RedirectToAction("Index1");
而不是:
return RedirectToAction("Index1", "NextFollowUp");
如果从其他控制器调用操作,请使用以下命令:
RedirectToAction("Index1", "NextFollowUp");
或者尝试以下代码:
return View("ActionName");
像这样:
if (DepartmentID == new Guid("47D2C992-1CB6-44AA-91CA-6AA3C338447E") &&
(UserTypeID == new Guid("106D02CC-7DC2-42BF-AC6F-D683ADDC1824") ||
(UserTypeID == new Guid("B3728982-0016-4562-BF73-E9B8B99BD501"))))
{
return View("Index1");
}
else
{
return View("Index2");
}
您不能重定向到标记为private
的方法。您需要将您的方法公开
public ActionResult Index1()
{
return View();
}
public ActionResult Index2()
{
return View();
}
假设您的控制器名为NextFollowUpController,它应该可以工作。点击F12并查看网络活动,检查它试图重定向到的确切Url。是的,我的控制器是该图像中提到的NextFollowup控制器,我使用调试点进行了检查。它没有达到索引1操作您需要使用浏览器调试器,F12您的方法是private
。使它们成为公共的(您不能重定向到私有方法)@snehasus您可以在VisitCount actionresult中添加return View()