成功验证表单后,我无法重定向表单。请帮我。我是ASP.NET和MVC Concepts的新手。 我在下面给出了模型,视图和控制器。索引页面显示登录信息,我将表单提交到同一页面。如果没有错误,我必须将表单重定向到另一个页面。这就是我想要做的。但即使我提供有效的登录信息,表单也不会重定向到指定的页面。
模型
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 -->
答案 0 :(得分:2)
您必须实际返回 RedirectToAction
来电的结果。在控制器RedirectToAction
方法中将return RedirectToAction
更改为HttpPost
:
[HttpPost]
public ActionResult Index(LoginModel model)
{
if (ModelState.IsValid)
{
return RedirectToAction("Transfer", "Home");
}
return View(model);
}