【问题标题】:Controller doesn't redirect after form submit表单提交后控制器不重定向
【发布时间】:2013-05-22 23:32:51
【问题描述】:

我是 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/Index 时卡在/Home/Add。但是视图会加载,因此它显示 /Home/Index,但 URL 显示 /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>
<% } %>

所以数据被发送到数据库并存储,但是重定向被破坏了。

【问题讨论】:

  • 然后抛出异常。在你的 catch 块中放置一个断点。
  • 是的,您似乎遇到了异常,正在执行您的 catch 块return View(obj);
  • 它执行得很好,数据被保存,并且正在执行重定向,地址 url 没有改变,这会弄乱应用程序,但是它会加载视图 Index()。
  • 会不会和 jQuery mobile 有关系?我也在使用它,但对框架很陌生
  • 是的,这是在我使用断点确定它正在触发之前发布的。事实证明问题出在 jQuery mobile 上,在使用它作为参考进行一些搜索后,我发现这个线程修复了我的问题错误:stackoverflow.com/questions/7824243/…

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2016-10-22
    • 2020-07-14
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多