[英]Call controller from another controller and return a view
I call an Action from a Login controller to authenticate users, once the user is authenticated I would like to call either the Cashier or the Supervisor action, depending on the user's role, and display the appropriate view. 我从登录控制器中调用一个操作来对用户进行身份验证,一旦用户通过身份验证,我想根据用户角色来调用收银员或主管操作,并显示适当的视图。
I can break on AuthenticateUserByCard
but RedirectToAction
doesn't seem to be working. 我可以在
AuthenticateUserByCard
断开,但RedirectToAction
似乎不起作用。
I'm not sure if what I'm trying to do is deviating from the MVC architecture, if so please suggest the correct way to do this 我不确定我要做的工作是否偏离MVC架构,如果是,请提出正确的做法
Login controller: 登录控制器:
public class LoginController : Controller
{
public ViewResult Index()
{
return View();
}
[HttpPost]
public ActionResult AuthenticateUserByCard(string token)
{
//Authenticate user and redirect to a specific view based on the user role
Role role = GetRoleByToken(token);
if(role.UserType == UserType.Supervisor)
return RedirectToAction("Supervisor", "Login", new { id = token });
else
return RedirectToAction("Cashier", "Login", new { id = token });
return null;
}
public ActionResult Supervisor(string id)
{
//Do some processing and display the Supervisor View
return View();
}
public ActionResult Cashier(string id)
{
//Do some processing and display the Cashier View
return View();
}
}
Java Script: Java脚本:
$.get("/Login/AuthenticateUserByCard",{token:token});
jQuery post
and get
ignore 301 browser redirects returned from the server. jQuery的
post
,并get
忽略301浏览器从服务器返回的重定向。 You would normally need to handle them yourself. 您通常需要自己处理。 This can get messy: How to manage a redirect request after a jQuery Ajax call
这可能会变得混乱: jQuery Ajax调用后如何管理重定向请求
All you really need in this case is to return the choice of methods, but make them return explicit views (not implicit). 在这种情况下,您真正需要的就是返回方法的选择,但要使它们返回显式视图(而不是隐式视图)。 The default would always be to return the view based on the IIS-called method ie "AuthenticateUserByCard" unless you specify the view.
默认情况下,除非您指定视图,否则始终始终基于IIS称为“ AuthenticateUserByCard”的方法返回视图。
public class LoginController : Controller
{
public ViewResult Index()
{
return View();
}
[HttpPost]
public ActionResult AuthenticateUserByCard(string token)
{
//Authenticate user and redirect to a specific view based on the user role
Role role = GetRoleByToken(token);
if(role.UserType == UserType.Supervisor)
return Supervisor(token);
else
return Cashier(token);
return null;
}
public ActionResult Supervisor(string id)
{
//Do some processing and display the Supervisor View
return View("Supervisor");
}
public ActionResult Cashier(string id)
{
//Do some processing and display the Cashier View
return View("Cashier");
}
This will not change the URL though. 但是,这不会更改URL。 If you need that too try the other answer I linked.
如果您也需要,请尝试我链接的其他答案。 You basically handle the redirect in jQuery and goto the new page.
您基本上可以在jQuery中处理重定向并转到新页面。
Alternatively, to change the URL, put the desired URL into a hidden field of the returned views and extract that value to update the browser URL (just a thought) :) 或者,要更改URL,请将所需的URL放入返回的视图的隐藏字段中,并提取该值以更新浏览器URL(仅是一个想法):)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.