Neil
Neil

Reputation: 2519

Controller doesn't redirect after form submit

I'm new to ASP.net MVC and I am struggling to make this work at the moment. I have a controller method called Add, it looks like this:

public ActionResult Add()
{
    // check user is authenticated
    if (Request.IsAuthenticated)
    {
        return View();
    }

    return RedirectToAction("Index", "Home");
}

//
// POST: /Home/Add

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Add(string title, string description, string priority, string color, FormCollection collection)
{
    if (ModelState.IsValid)
    {
        // create instance of todo object
        todo obj = new todo();

        try
        {
            // gather fields
            obj.priority = Convert.ToInt32(priority);
            obj.color = Convert.ToInt32(color);
            obj.title = title;
            obj.description = description;

            todosDataContext objLinq = new todosDataContext();

            // get the users id, convert to string and store it
            var userid = Membership.GetUser().ProviderUserKey;
            obj.userid = userid.ToString();

            // save
            objLinq.todos.InsertOnSubmit(obj);
            objLinq.SubmitChanges();

            return RedirectToAction("Index", "Home");
        }
        catch
        {
            return View(obj);
        }
    }

    return RedirectToAction("Index", "Home");
}

If data is sent via POST to the method, it should add the data to the database. That is working fine and everything is added correctly. However, the RedirectToAction is not firing, and the application gets stuck at /Home/Add, when it should redirect to /Home/Index. The view loads however, so it shows /Home/Index but the URL says /Home/Add.

Here is a copy of the partial view that contains the form:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<todo_moble_oauth.Models.todo>" %>

<% using (Html.BeginForm()) { %>
<%: Html.AntiForgeryToken() %>
<%: Html.ValidationSummary(true) %>

<fieldset>

    <h3>Title:</h3>
    <div class="editor-field">
        <input type="text" name="title" />
    </div>

    <h3>Description:</h3>
    <div class="editor-field">
        <input type="text" name="description" />
    </div>

    <h3>Priority:</h3>
    <div class="editor-field">
        <select name="priority">
            <option value="1">Low</option>
            <option value="2">Medium</option>
            <option value="3">High</option>
        </select>
    </div>

    <div data-role="fieldcontain">
        <fieldset data-role="controlgroup">
            <h3>Color:</h3>
            <input type="radio" name="color" id="radio-choice-1" value="0" checked="checked" />
            <label for="radio-choice-1">None</label>

            <input type="radio" name="color" id="radio-choice-2" value="1"  />
            <label for="radio-choice-2">Red</label>

            <input type="radio" name="color" id="radio-choice-3" value="2"  />
            <label for="radio-choice-3">Blue</label>

            <input type="radio" name="color" id="radio-choice-4" value="3"  />
            <label for="radio-choice-4">Yellow</label>
        </fieldset>
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>

</fieldset>
<% } %>

So data is being sent to the database and stored, however the redirect is broken.

Upvotes: 1

Views: 1499

Answers (1)

Neil
Neil

Reputation: 2519

Turns out it is an issue with jQuery mobile, this threads solution resolved the issue for me:

jQuery Mobile/MVC: Getting the browser URL to change with RedirectToAction

Upvotes: 2

Related Questions