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调用结果。更改RedirectToActionreturn RedirectToAction在您的控制器HttpPost方法中:

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

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