My task is to create a registration and registration form in one submission!
I have a home look. On the home screen, I have two forms: login and registration. They are displayed using @ Html.RenderPartial (partial name, model). These forms apply to the account controller (login and registration actions).
Let me first give you the code ...
Models
// Login model, that contains email and password public class Login { β¦ } // Registration model that contains minimum user data, eg name, surname public class Register { ... } // Model for home view which contains models for both register and login public class Home { public Login LoginModel { get; set; } public Register RegisterModel { get; set; } }
Views : Index.cshtml
@model App.Models.Home.Home @{ Html.RenderPartial("_LoginArea", Model.LoginModel); } @{ Html.RenderPartial("_RegisterArea", Model.RegisterModel); }
_LoginArea.cshtml
@model App.Models.Account.Login <div style="float: left; margin-right: 100px;"> <h1>@Localization.Account.Login</h1> <div id="loginBox"> @using (Html.BeginForm("Login", "Account", FormMethod.Post)) { <div class="editor-label"> @Html.LabelFor(m => m.Email): </div> <div class="editor-field"> @Html.TextBoxFor(m => m.Email) @Html.ValidationMessageFor(m => m.Email) </div> <div class="editor-label"> @Html.LabelFor(m => m.Password): </div> <div class="editor-field"> @Html.PasswordFor(m => m.Password) @Html.ValidationMessageFor(m => m.Password) </div> <div class="editor-label"> @Html.CheckBoxFor(m => m.RememberMe) @Html.LabelFor(m => m.RememberMe) </div> <p> <input type="submit" value="@Localization.Account.Login" /> </p> } </div> </div>
_RegisterArea.cshtml
@model App.Models.Account.Register <div style="vertical-align: bottom;"> <h1>@Localization.Account.Register</h1> <div> @using (Html.BeginForm("Register", "Account")) {
Controllers
Homecontroller
// // GET: /Home/ public ActionResult Index(Home model) { // // If logedin redirect to profile page // Else show home page view // if (Request.IsAuthenticated) { return RedirectToAction("Index", "User", new { id = HttpContext.User.Identity.Name }); } else { model.LoginModel = new Login(); model.RegisterModel = new Register(); return View(model); } }
Just show the login action using the account controller
// // POST: /Account/Login [HttpPost] public ActionResult LogIn(Login model) { if (Request.IsAuthenticated) { return RedirectToAction("Index", "User", new { id = HttpContext.User.Identity.Name }); } else { if (ModelState.IsValid) { if (model.ProcessLogin()) { return RedirectToAction("Index", "User", new { id = HttpContext.Session["id"] }); } } } //Note: this is problem part // If we got this far, something failed, redisplay form return View("~/Views/Home/Index.cshtml", model); }
So everything works fine, BUT ! When the state of the model is invalid, I have the following exception
The model item passed into the dictionary is of type 'App.Models.Account.Login', but this dictionary requires a model item of type App.Models.Home.Home'.
I can bind these partial views and actions of the account to the Home model, but this is not what I need. I plan to use the _LoginArea.cshtml view in other views (for example, in the header of the "Custom view" page, and this view has a different model) So I need an action method (like Login) to retrieve the login model, not Home or whatever something else. But in this case, Im could not return the model to the Home view. How can I solve this problem? What is the best fit? Sorry for the big code, I just want clear things.