asp.net-mvc 同一页面中的多个表单 ASP.net MVC
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9997874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple Forms in same page ASP.net MVC
提问by Chlebta
I working on my first ASP.net MVC project and and i got some problems using multi forms in same page. First i have created 2 partial Class : (*Register will allow user to register, *Login it allow user to login.)
我在做我的第一个 ASP.net MVC 项目,并且在同一页面中使用多个表单时遇到了一些问题。首先我创建了 2 个部分类:(*注册将允许用户注册,*登录它允许用户登录。)
Then i used HTML.render to integrate them in my "Logpage". So i have To uses 2 different Action. Like this one:
然后我使用 HTML.render 将它们集成到我的“日志页面”中。所以我必须使用 2 个不同的动作。像这个:
[HttpPost]
public ActionResult Login(LogModel.Login Model)
{
if (ModelState.IsValid)
{
if (LogModel.Login.Verifuser(Model.IDUser, Model.Password))
{
FormsAuthentication.SetAuthCookie(Model.IDUser, false);
if (LogModel.Login.IsAdmin(Model.IDUser, Model.Password))
{
return View("Admin/Index");
}
else
{
return View("Agence/Index");
}
}
else
{
ModelState.AddModelError("", "Invalide username or Password");
return View(Model);
}
}
return View(Model);
}
The problem that on error case i'm redirect to new Page(White page contain validation summary). So i'm wondring how to show this error message in my default page Logpage.
在错误情况下我重定向到新页面的问题(白页包含验证摘要)。所以我想知道如何在我的默认页面 Logpage 中显示此错误消息。
回答by Nick Bork
You can solve this with three actions and a complex model.
您可以通过三个动作和一个复杂的模型来解决这个问题。
public class LoginOrRegisterViewModel
{
public Models.RegisterViewModel Register { get; set; }
public Models.LoginViewModel Login { get; set; }
}
[HttpGet]
public ActionResult Login()
{
return View("Login", new LoginOrRegisterViewModel());
}
[HttpPost]
public ActionResult Register(Models.LoginViewModel model)
{
if(!ModelState.IsValid)
return View("Login", new LoginOrRegisterViewModel(){ Register = model });
else
{
//TODO: Validate the user
//TODO: Write a FormsAuth ticket
//TODO: Redirect to somewhere
}
}
[HttpPost]
public ActionResult Login(Models.RegistrationViewModel model)
{
if(!ModelState.IsValid)
return View("Login", new LoginOrRegisterViewModel(){ Login = model});
else
{
//TODO: CRUD for registering user
//TODO: Write forms auth ticket
//TODO: Redirect
}
}
In your code, make sure that you set the action of the Form:
在您的代码中,确保您设置了表单的操作:
@model Models.LoginOrRegisterViewModel
@using(Html.BeginForm("Login", "Controller", FormMethod.Post, new { id = "loginForm"}))
{
@Html.EditorFor(m => Model.Login)
}
@using(Html.BeginForm("Register", "Controller", FormMethod.Post, new { id = "registerForm"}))
{
@Html.EditorFor(m => Model.Register)
}
回答by Tuan
It looks like you also need to call IsAdmin in the !Verifyuser branch, to have it return the correct view:
看起来您还需要在 !Verifyuser 分支中调用 IsAdmin 以使其返回正确的视图:
if (ModelState.IsValid)
{
if (LogModel.Login.Verifuser(Model.IDUser, Model.Password))
{
FormsAuthentication.SetAuthCookie(Model.IDUser, false);
if (LogModel.Login.IsAdmin(Model.IDUser, Model.Password))
{
return View("Admin/Index");
}
else
{
return View("Agence/Index");
}
}
else
{
ModelState.AddModelError("", "Invalide username or Password");
if (LogModel.Login.IsAdmin(Model.IDUser, Model.Password))
return View("Admin/Index", Model);
else
return View("Agence/Index", Model);
}
}
Currently, you're calling return View(Model);when you have a ModelState error, which returns the default view for the action, called "Login".
当前,您在return View(Model);遇到 ModelState 错误时调用,该错误返回操作的默认视图,称为“登录”。

