表单提交后,Controller不会重定向

时间:2013-05-22 23:32:52

标签: asp.net asp.net-mvc asp.net-mvc-4

我是ASP.net MVC的新手,我现在正在努力完成这项工作。我有一个名为Add的控制器方法,它看起来像这样:

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");
}

如果数据通过POST发送到方法,它应该将数据添加到数据库。这工作正常,一切都正确添加。但是,RedirectToAction未触发,应用程序停留在/Home/Add,应该重定向到/Home/Index。但是视图会加载,因此会显示/Home/Index,但网址会显示/Home/Add

以下是包含表单的部分视图的副本:

<%@ 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>
<% } %>

因此数据被发送到数据库并存储,但重定向被破坏。

1 个答案:

答案 0 :(得分:2)

原来这是jQuery mobile的一个问题,这个线程解决方案为我解决了这个问题:

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