1

我是 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,但 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>
<% } %>

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

4

1 回答 1

2

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

jQuery Mobile/MVC:使用 RedirectToAction 获取浏览器 URL

于 2013-05-23T00:07:41.427 回答