Psyfun
Psyfun

Reputation: 369

MVC 4 RedirectToAction Generates Wrong URL

I am having a problem with RedirectToAction. I have the following simple controller class. Index method displays a list of Groups. Create creates a new group and adds it to the database. This works fine and shows up in the list when it is redirected back to Index. The problem is that the URL once it renders Index is still using the one from the Create: /Group/Create. I think it is actually redirecting to Index properly and then immediately flashing to the Create action because it shows the right content for Index. Any idea what would cause this? I have no custom routes defined, so I am pretty sure it isn't a routing issue. I suspect it is an AJAX problem.

public class GroupController : Controller
{
    private ModelDb db = new ModelDb();

    [Authorize(Roles = "Administrator")]
    public ActionResult Index()
    {
        return View();
    }

    [Authorize(Roles = "Administrator")]
    public ActionResult Create()
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    [Authorize(Roles = "Administrator")]
    public ActionResult Create(Group group)
    {
        if (ModelState.IsValid)
        {
            db.Groups.Add(group);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(group);
    }
}

Upvotes: 0

Views: 283

Answers (1)

Psyfun
Psyfun

Reputation: 369

Turns out it was an AJAX problem created by JQuery Mobile. I should have done more testing before posting the question, but maybe it will help someone else. I disabled posting the data using AJAX and the problem went away. Now I have to update a bunch of forms to disable it everywhere.

So I now use the following code to begin a form:

@using (Html.BeginForm("Create", "Group", FormMethod.Post, new { data_ajax = "false" }))

Upvotes: 0

Related Questions