多个表单在同一个页面ASP.net MVC

时间:2022-07-28 16:49:14

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个部分类:(*Register允许用户注册,*Login允许用户登录)。

Then i used HTML.render to integrate them in my "Logpage". So i have To uses 2 different Action. Like this one:

然后我使用HTML。渲染将它们集成到我的“Logpage”中。我要用两个不同的动作。像这样:

        [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.

在错误情况下,我重定向到新页面(白色页面包含验证摘要)。因此,在我的默认页面日志中,我想知道如何显示这个错误消息。

2 个解决方案

#1


24  

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)
 }

#2


0  

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错误时,它返回操作的默认视图,称为“Login”。

#1


24  

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)
 }

#2


0  

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错误时,它返回操作的默认视图,称为“Login”。