On a MVC controller I have the following:
public virtual ActionResult Test() {
ActionResult result = MVC.Home.Index();
return RedirectToAction(result);
return View();
}
This redirects to result. View is never returned.
Now I need to do something as follows:
public virtual ActionResult Test() {
ActionResult result = MVC.Home.Index();
MyClass.RedirectTo(result, this);
return View();
}
Where MyClass.RedirectTo method is the following:
public static void RedirectTo(ActionResult result, Controller controller) {
controller.Response.Redirect(result.ToString());
}
But in this case the redirection does not happen.
The View is returned. Does anyone knows how to do this?
Hi you have to redirect to a specific action on a specific controller
e.g
return RedirectToAction("Index", "ControllerName");
Related
I have a controller action Foo. From within Foo I need to pass a GUID to another action called Index. how can I do that?
public ActionResult Foo() {
return View("Index",someguid);
}
public ActionResult Index(Guid id)
How can I do that?
Precisely what the RedirectToAction method is for.
As Dom mentioned in the comments, this should get you going in the right direction.
public ActionResult Foo() {
return RedirectToAction("Index", new { id = someguid });
}
I am working on a ASP.NET MVC website and I am new to this.
I have a controller with few actions. I want to use these actions through out my website.
For example
[HttpPost]
public ActionResult MyAction(ViewModel model)
{
if (ModelState.IsValid)
{
//code is here
}
return RedirectToAction(); // redirect to same view
}
I want to redirect to same view from where request is generated. I am not sure if this is possible or not ?
Based on your comment, I would create a Controller that looks like:
public MyController : Controller
{
private ActionResult SharedMethod(SomeModel model)
{
if (ModelState.IsValid)
{
//code is here
}
// viewname is required, otherwise the view name used will be
// the original calling method (ie public1.cshtml, public2.cshtml)
return this.View("SharedViewName");
}
public ActionResult Public1(SomeModel model)
{
return this.SharedMethod(model);
}
public ActionResult Public1(SomeModel model)
{
return this.SharedMethod(model);
}
}
I am trying to redirect to action and get a new view (a new page) with no success. While debugging, I'm reaching the controller but not getting the view (the page URL is not changed).
With Fiddler I see that the page returns the right view result but in the browser the URL is not changed!
When shopping cart is empty, I would like to redirect to a new page a display the error message.
[HttpPost]
public RedirectToRouteResult PlaceOrder(DeliveryDetails deliveryDetails)
{
if (UserCart.IsEmpty)
{
TempData["errorMsg"] = "Error: Cart is empty";
return RedirectToAction("Index", "Error");
}
else
{
InsertOrder();
}
}
ErrorController:
public ActionResult Index()
{
return View();
}
ErrorController View:
#TempData["errorMsg"]
Thanks.
Then I would surmise that UserCart.IsEmpty is evaluating to false. What does your Error Index route look like? Also, you're better off returning a base ActionResult from a controller action in case you need to return a view. Presumably there's more code in the PlaceOrder method because that won't compile as it stands
Use ActionResult instead of RedirectToRouteResult
[HttpPost]
public ActionResult PlaceOrder(DeliveryDetails deliveryDetails)
{
// .....
return RedirectToAction("Index", "Error");
}
I want to know if controller can fully replace http handler when no view involved. The function looks similar.
Sure:
public ActionResult Index()
{
return Content("No view involved here", "text/plain");
}
or:
public ActionResult Index()
{
return File("test.pdf", "application/pdf");
}
or:
public ActionResult Index()
{
return Json(new { foo = "bar" });
}
In all those examples, there's no view involved. The controller acts as an HTTP handler.
So basically in my UserController.cs class I have an Index method that returns the ActionResult to display the dashboard for the user. On this page is a html button with a type of submit. When I hit this button I want to capture it on the server side log the user out.
How can I do this since I'm not passing information back and the method signature ends up being the same.
Thanks,
Mike
[Authorize]
public ActionResult Index()
{
return View();
}
[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
Use ActionNameAttribute:
[AcceptVerbs(HttpVerbs.Get), ActionName("Index"), Authorize]
public ActionResult IndexGet()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post), ActionName("Index"), Authorize]
public ActionResult IndexPost()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
Use:
[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection values)
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
Or
[Authorize]
[AcceptVerbs(HttpVerbs.Post), ActionName("Post")]
public ActionResult IndexPost()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
As you know in C# you cannot have two methods with the same name and same arguments within the same class. You could either add some dummy parameter or I would advice you to rename the second action to SignOut which seems more semantically correct and better reflecting what this action actually does.