Scott
Scott

Reputation: 11186

RedirectToAction isn't working correctly

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.

Upvotes: 0

Views: 890

Answers (1)

Scott
Scott

Reputation: 11186

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

Upvotes: 1

Related Questions