i am trying to redirect to another action within the same controller
action is called index
[HttpGet]
public ActionResult Search(string city)
{
return RedirectToAction("Index", "Rentals", new { CityName = city });
}
this is index action
[HttpPost]
public ActionResult Index(String CityName)
{
}
am i missing something?
You are trying to redirect action which is searching for a matching action but in this case there is no get action, so you have to add a get method to accept redirect. If you want, you can check the HTTPGET or POST inside the method
[HttpPost]<---- Remove this
public ActionResult Index(String CityName)
{
}
Please change HttpPost to HttpGet
[HttpGet]
public ActionResult Index(String CityName)
{
}
Because whenever you call the Action, then the GET method will be first called.
As the two actions are in the same controller, you could call the Index method directly from Search like this:
return Index(city);
not necessarily to user the RedirectToAction method.
Related
I want to check for CSRF in my webapp using the [ValidateAntiForgeryToken] attribute. The thing is, I have a GET method that changes data, so it should actually be a POST. But aside from a submit to this action method in the corresponding form, there are multiple redirects to this method, so I can't change it to a POST method. As far as I know, the [ValidateAntiForgeryToken] doesn't work for GET methods. Is there another way to validate a GET method or to redirect within the code to a POST method without using a form?
My action method looks something like this:
public ActionResult SomeAction(SomeModel model)
{
// changes are made in database!!
return View("View", model);
}
And this action method is being redirected to from another action method:
public ActionResult SomeOtherAction()
{
return RedirectToAction("SomeAction", "Controller");
}
I would like to change the first action to:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SomeAction(SomeModel model)
{
// changes are made in database!!
return View("View", model);
}
and add #Html.AntiForgeryToken in the corresponding view. But then the redirect in the second action won't work anymore. Does anyone know a way out of this?
You could return the someAction method...
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpGet]
[ValidateAntiForgeryToken]
public ActionResult SomeOtherAction()
{
return SomeAction();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SomeAction()
{
return View("SomeAction");
}
}
Index.cshtml
#using (Html.BeginForm("SomeOtherAction", "Home", FormMethod.Get))
{
#Html.AntiForgeryToken();
<button type="submit">Click Me</button>
}
This is two action method in controller with same name, i want to change this action name using attribute.
[HttpGet]
public ActionResult Show()
{
return View();
}
[HttpPost]
public ActionResult Show(FormCollection frm)
{
return View();
}
You can use ActionName attribute.
[HttpPost, ActionName("Show")]
public ActionResult PostShow()
{
// your code...
}
You can have the same name, but make sure that the method signature is different. To do that, you can simply add a parameter to your post method.
[HttpGet]
public ActionResult Show()
{
return View();
}
[HttpPost]
public ActionResult Show(string name)
{
return View();
}
Now when the Show form is submitted the input field with name value name will be submitted to the HttpPost action method.
I am attempting to make a simple test website to allow me to list, create, edit and delete customer objects using MVC4.
Inside my controller I have 2 create methods, a Get for when the form loads with the controls, and a Post that actually saves the data.
//
// GET: /Customer/Create
[HttpGet]
public ActionResult Create()
{
return View();
}
//
// POST: /Customer/Create
[HttpPost]
public ActionResult Create(Customer cust)
{
if (ModelState.IsValid)
{
_repository.Add(cust);
return RedirectToAction("GetAllCustomers");
}
return View(cust);
}
However when I run the project and attempt to access the create action I get an error that:
The current request for action 'Create' on controller type 'CustomerController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Create() on type [Project].Controllers.CustomerController
System.Web.Mvc.ActionResult Create([Project].Models.Customer) on type [Project].Controllers.CustomerController
My I understand that it can't see the difference between my Get and Post methods, but I have added the attribues. What could be the cause of this and how can I make it work again?
MVC does not authorize you to have 2 action methods with the same name.
BUT you can have 2 action methods with the same URI when the http verb differs (GET, POST). Use the ActionName attribute to set the action name. Don't use the same methods names. You can use any name. A convention is to add the http verb as the method suffix.
[HttpPost]
[ActionName("Create")]
public ActionResult CreatePost(Customer cust)
{
if (ModelState.IsValid)
{
_repository.Add(cust);
return RedirectToAction("GetAllCustomers");
}
return View(cust);
}
I'm beginner in MVC3, and I want to get a value from an another controller's method. Here the two methods:
[HttpPost]
public ActionResult Create(TennisClub tennisclub)
{
if (ModelState.IsValid)
{
db.TennisClubs.Add(tennisclub);
db.SaveChanges();
return RedirectToAction("AssignManager");
}
return View(tennisclub);
}
[HttpPost]
public ActionResult AssignManager(Manager manager)
{
}
So, when I'm creating a new tennis club, Immediately I would like to assign a manager to it... For that I need the primary key "ID".
So my question is: How to get this ID in my "AssignManager" method ? Thanks in advance
You cannot redirect to an action decorated with the [HttpPost] attribute. That's not how a redirect works. A redirect means that you are sending a 301 HTTP status code to the client with the new Location header and the client issues a GET request to this new location.
So once you remove the [HttpPost] attribute from your AssignManager action you could pass the id as parameter:
return RedirectToAction("AssignManager", new { id = "123" });
and then:
[HttpPost]
public ActionResult AssignManager(int id)
{
}
Basically, you need to have a GET AssignManager method, too, which would have a parameter telling it to which TennisClub the manager should be assigned:
[HttpGet]
public ActionResult AssignManager(int tennisClubId)
{
// here, you will want to return AssignManager view
}
And when redirecting to AssignManager from Create, you can specify the id of TennisClub:
return RedirectToAction("AssignManager", new { tennisClubId = tennisclub.Id });
return RedirectToAction("AssignManager", new { id = tennisclub.Id });
Also you need to remove the [HttpPost] attribute from your action
public ActionResult AssignManager(int id) {
//...
}
I have a simple MVC2 app that doesn't seem to Redirect correctly. The code is setup as follows:
[HttpPost]
[Authorize]
public ActionResult QuickAddEvent(CalendarEvent calEvent)
{
if (ModelState.IsValid)
{
int eventID = repo.AddEvent(calEvent);
return RedirectToAction("Event", new { id = eventID });
}
return RedirectToAction("Index", "Home");
}
[ChildActionOnly]
public ActionResult QuickAddEvent()
{
return PartialView();
}
public ActionResult Event(int id)
{
CalendarEvent curEvent = repo.ByID(id);
return View(curEvent);
}
The problem I am running into is that no matter what the ModelState is on HttpPost the page redirects to itself. That is, no matter what the model state is, I always end up at /EventCalendar/Index instead of one of the two specified actions.
Since QuickAddEvent is returning a PartialView, the form action isposting to /EventCalendar/Index and not /EventCalendar/QuickAddEvent. The fix is changing the action name for the [httpPost] to index