Upon successful validation of the form, I'm unable to redirect the form. Please help me. I'm very new to ASP.NET and MVC Concepts. I have given below the model, the view and the controller. The Index page displays login information, and I'm submitting the form to the same page. If there are no errors, I have to redirect the form to a different page. This is what I'm trying to do. But even If I provide valid login information, the form doesn't redirect to the specified page.
成功验证表单后,我无法重定向表单。请帮助我。我对ASP很陌生。净和MVC概念。我在下面给出了模型,视图和控制器。索引页显示登录信息,我将表单提交到相同的页面。如果没有错误,我必须将表单重定向到另一个页面。这就是我要做的。但是即使我提供了有效的登录信息,表单也不会重定向到指定的页面。
Model
模型
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;
}
}
}
Controller
控制器
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();
}
}
}
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 -->
1 个解决方案
#1
2
You have to actually return the result of the RedirectToAction
call. Change RedirectToAction
to be return RedirectToAction
in your controller HttpPost
method:
实际上,您必须返回RedirectToAction调用的结果。在您的控制器HttpPost方法中,将RedirectToAction更改为return RedirectToAction:
[HttpPost]
public ActionResult Index(LoginModel model)
{
if (ModelState.IsValid)
{
return RedirectToAction("Transfer", "Home");
}
return View(model);
}
#1
2
You have to actually return the result of the RedirectToAction
call. Change RedirectToAction
to be return RedirectToAction
in your controller HttpPost
method:
实际上,您必须返回RedirectToAction调用的结果。在您的控制器HttpPost方法中,将RedirectToAction更改为return RedirectToAction:
[HttpPost]
public ActionResult Index(LoginModel model)
{
if (ModelState.IsValid)
{
return RedirectToAction("Transfer", "Home");
}
return View(model);
}