1

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Neil
  • 2,509
  • 7
  • 32
  • 47
  • 1
    It throws an exception then. Put a breakpoint inside your catch block. – Ufuk Hacıoğulları May 22 '13 at 23:38
  • Yes, it appears you have an exception and are executing your catch block `return View(obj);` – Jasen May 22 '13 at 23:51
  • It is executing fine, data is saved, and the redirect is being performed, the address url doesn't change which messes up the application, however it loads the view Index(). – Neil May 22 '13 at 23:58
  • Could it be something to do with jQuery mobile? I'm using it as well, but am new to the framework – Neil May 22 '13 at 23:59
  • Then I misunderstood *"However, the `RedirectToAction` is not firing"* – Jasen May 23 '13 at 00:02
  • 1
    Yes, that was posted before I used a breakpoint to determine it was firing.. turns out the issue is with jQuery mobile, after doing some searching using it as a reference I found this thread which fixed my error: http://stackoverflow.com/questions/7824243/jquery-mobile-mvc-getting-the-browser-url-to-change-with-redirecttoaction – Neil May 23 '13 at 00:07
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 23 '13 at 02:41

1 Answers1

2

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

Community
  • 1
  • 1
Neil
  • 2,509
  • 7
  • 32
  • 47