如何从控制器上下文中返回另一个动作
-
24-10-2019 - |
题
这是我当前的代码,我正在使用它来实现选项卡
public ActionResult Index(string tabs, int id = 0)
{
switch ((Tabs)Enum.Parse(typeof(Tabs), tabs,true))
{
case Tabs.Profile:
default:
return Profile(id);
}
}
public ActionResult Profile(int id = 0)
{
User user = UsersRepository.GetUser(id);
if (user!= null)
{
return View(user);
}
return Redirect("/");
}
我不想使用 RedirectToAction
因为那将改变我想要的URL结构。这样的事情:
http://localhost/user?tabs=profile
http://localhost/user?tabs=settings
解决方案
这是我目前的看法,这对我来说是不自然的
public ActionResult Index(string tabs, int id = 0)
{
switch ((Tabs)Enum.Parse(typeof(Tabs), tabs,true))
{
case Tabs.Profile:
default:
var userProfile= Profile(id);
if (userProfile!= null)
{
return View("Profile",userProfile);
}
return Redirect("/");
}
}
[NonAction]
public UsersViewModel Profile(int id = 0)
{
UsersViewModel user= UsersRepository.GetUser(id);
return user;
}
不隶属于 StackOverflow