【问题标题】:How to call an ActionResult from another ActionResult in asp.net如何从 asp.net 中的另一个 ActionResult 调用 ActionResult
【发布时间】:2020-03-28 00:13:23
【问题描述】:

我正在创建一个用于用户注册、显示、登录等的网站。我目前正在尝试显示已登录用户的详细信息。但是在登录的 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;
    }
}

【问题讨论】:

  • 正确标记您的问题,使其成为适当用户的焦点,并花一些时间格式化您的问题

标签: asp.net-mvc


【解决方案1】:

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

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

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

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

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

或者如果需要使用[HttpPost],可以学习如何使用 RedirectToAction to a POST Action.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多