2

フォームの検証に成功すると、フォームをリダイレクトできません。私を助けてください。私はASP.NETとMVCの概念に非常に慣れていません。モデル、ビュー、コントローラーを以下に示します。インデックスページにログイン情報が表示され、同じページにフォームを送信しています。エラーがない場合は、フォームを別のページにリダイレクトする必要があります。これが私がやろうとしていることです。ただし、有効なログイン情報を入力しても、フォームは指定されたページにリダイレクトされません。

モデル

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MyProject.Models
{
  public class LoginModel
  {  
    [Required(ErrorMessage = "UserCode is Required.")]
    public string UserCode
    {
        get;
        set;
    }

    [DataType(DataType.Password)]
    [Required(ErrorMessage = "Password is Required.")]
    public string Password
    {
        get;
        set;
    }
  }
}

コントローラ

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyProject.Models;

namespace MyProject.Controllers
{
[HandleError]
  public class HomeController : Controller
  {
    // GET
    public ActionResult Index()
    {
        return View();
    }

    // POST
    [HttpPost]
    public ActionResult Index(LoginModel model)
    {
        if (ModelState.IsValid)
        {
            RedirectToAction("Transfer", "Home");
        }

        return View(model);
    }

    public ActionResult UpgradeBrowser()
    {
        return View();
    }
  }
}

意見

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"   Inherits="System.Web.Mvc.ViewPage<MyProject.Models.LoginModel>" %>


<div id="LoginBox">
<% using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "FrmLoginUser" }))
   { %>
    <table class="TblForm">
        <tr>
            <td><label for="UserName">UserCode</label></td>
            <td><%= Html.TextBox("UserCode", "", new { id = "UserCode" })%></td>
            <td><%= Html.ValidationMessage("UserCode", new { @class = "ValidationError" })%></td>
        </tr>
        <tr>
            <td><label for="Password">Password</label></td>
            <td><%= Html.Password("Password", "", new { id="Password" })%></td>
            <td><%= Html.ValidationMessage("Password", new { @class = "ValidationError" })%></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Login" /></td>
        </tr>
    </table>
<% } %>

</div> <!-- #LoginBox -->
4

1 に答える 1

2

実際にRedirectToAction呼び出しの結果を返す必要があります。コントローラメソッドに変更RedirectToActionします。return RedirectToActionHttpPost

[HttpPost]
public ActionResult Index(LoginModel model)
{
    if (ModelState.IsValid)
    {
        return RedirectToAction("Transfer", "Home");
    }

    return View(model);
}
于 2013-01-24T10:06:00.633 に答える