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;
}
}
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.
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With