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モバイルの問題であることが判明しました.このスレッドソリューションは私にとって問題を解決しました:

jQuery Mobile/MVC: RedirectToAction で変更するブラウザー URL を取得する

于 2013-05-23T00:07:41.427 に答える