asp.net-mvc 如何从asp.net中的另一个ActionResult调用ActionResult

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19829926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:18:12  来源:igfitidea点击:

How to call an ActionResult from another ActionResult in asp.net

asp.net-mvc

提问by Shanida

I am creating a website for User registeration,display,login etc. I am currently trying to display the details of the user who have signed in. But within the actionResult of login I don't know how will i call the actionResult of display? I am new to asp.net. I need suggestions

我正在创建一个用于用户注册、显示、登录等的网站。我目前正在尝试显示已登录用户的详细信息。但在登录的 actionResult 中,我不知道如何调用显示的 actionResult?我是asp.net的新手。我需要建议

public ActionResult login()
{
    try
    {
        return View();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

[HttpPost]     
public ActionResult login(DEntities.Users user)
{
    try
    {
        services.CheckUser(user);
        controlsuccess = services.servicesuccess;
        if (controlsuccess == true)
        {

            return RedirectToAction("display");             
            //return View("display");
        }
        else
        { 
            return HttpNotFound();
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

public ActionResult display()
{
    return View();
}

[HttpPost]
public ActionResult display(int id = 0)
{
    try
    {
        DEntities.Users user = services.GetUserbyId(id);
        return View(user);

    }
    catch (Exception ex)
    {
        throw ex;
    }
}

回答by Nirmal

Remove the [HttpPost]attribute from the displayaction.

[HttpPost]display操作中删除属性。

If both actions are in the same controller, then just pass the action name:

如果两个动作都在同一个控制器中,那么只需传递动作名称:

return RedirectToAction("display", new { id = 1 });

Or if the actions are in different controllers, pass the action and controller names:

或者如果动作在不同的控制器中,传递动作和控制器名称:

return RedirectToAction("display", "controllername", new { id = 1 });

Or if it is necessary to use [HttpPost], you can learn how to RedirectToActionto a POSTAction.

或者如果有必要使用[HttpPost],你可以学习如何 操作RedirectToAction一个POST动作