简体   繁体   English

从另一个控制器调用控制器并返回视图

[英]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”的方法返回视图。

eg 例如

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.

相关问题 如何从 ASP.NET Core MVC 中的一个控制器调用或返回另一个控制器的视图 - How to call or return the view of another controller from one controller in ASP.NET Core MVC 从另一个控制器问题返回视图 - Return view from another controller problem 如何通过控制器返回另一个控制器的部分视图? - How to Return partial view of another controller by controller? 如果从另一个视图的 ajax 调用启动,控制器不会返回视图 - Controller not returning view if initiated from ajax call from another view 从控制器重定向到另一个控制器的另一个视图 - Redirect from controller to another view of another controller MVC如何从视图中调用控制器中的操作以返回值 - MVC How to call an action in controller from view to return a value 来自javascript的调用控制器方法,可返回常规视图(非局部视图) - Call controller method from javascript that return regular view(not partial) 需要调用从视图返回String值的控制器方法 - need to call controller method which return String value from view 从另一个控制器调用WebApi控制器 - Call WebApi controller from another controller 如何从控制器中的另一个模型返回视图? - How can I return a view from another model in a controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM