Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

asp.net-mvc

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

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;
    }
}
like image 743
Shanida Avatar asked Nov 07 '13 07:11

Shanida


People also ask

How do you call one ActionResult from another ActionResult in MVC?

If both actions are in the same controller, then just pass the action name: return RedirectToAction("display", new { id = 1 }); return RedirectToAction("display", "controllername", new { id = 1 }); Or if it is necessary to use [HttpPost] , you can learn how to RedirectToAction to a POST Action.

How do you pass an object from one action to another action in MVC?

You can use TempData to pass the object. This worked well, Thanks Eranga. One potential pitfall is that the object can be disposed before it gets used in the view if you have a overridden definition for Dispose in your controller(something VS tends to add in with its auto generated CRUD code).

What is the difference between ViewResult () and ActionResult () in asp net MVC?

ActionResult is an abstract class, and it's base class for ViewResult class. In MVC framework, it uses ActionResult class to reference the object your action method returns. And invokes ExecuteResult method on it. And ViewResult is an implementation for this abstract class.

Can I return ActionResult instead of ViewResult?

First to Understand---ActionResult and ViewResult are basically a return type for an method(i.e Action in MVC). Second The Difference is --- ActionResult can return any type of Result Whereas ViewResult can return result type of View Only.


1 Answers

Remove the [HttpPost] attribute from the display action.

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 RedirectToAction to a POST Action.

like image 131
Nirmal Avatar answered Jan 03 '23 17:01

Nirmal