<%@ Page Language="C#" MasterPageFile="~/Views/Shared/MasterPage.Master" Inherits="System.Web.Mvc.ViewPage<EAZYITT_LOGIN.Models.CombinedViewModel>" %>
<asp:Content ContentPlaceHolderID="TitleContent" runat="server">
LoginPage
</asp:Content>
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
<div id="LoginWindow">
<% Html.RenderPartial("LoginWindow", ViewData.Model.Logon); %>
<a id="ForgetPassword" href="#" onclick="loadSegment()">Forgot Password</a>
</div>
<div id="PassReminderWindow">
<% Html.RenderPartial("ReminderWindow", ViewData.Model.Reminder); %>
</div>
</asp:Content>
Each partial view is strongly typed with separate postbacks to the server
Login:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<EAZYITT_LOGIN.Models.LogOnModel>" %>
<div id="loginPage">
<h2>Login Page</h2>
<h3>Submit your credentials to continue or register</h3>
<%: Html.ActionLink("New Registration", "Register")%>
<%: Html.ValidationSummary(true)%>
<%: Html.ValidationSummary()%>
<div class="validation-summary-errors">
<span id="loginError"></span>
</div>
<% using (Html.BeginForm("LoginWindow","Account",FormMethod.Post)) { %>
<%:Html.LabelFor(m =>m.EmailAddress) %>
<%:Html.ValidationMessageFor(m => m.EmailAddress) %>
<%:Html.TextBoxFor(m => m.EmailAddress) %>
<%:Html.LabelFor(m =>m.Password) %>
<%:Html.ValidationMessageFor(m => m.Password) %>
<%:Html.PasswordFor(m => m.Password)%>
<%:Html.CheckBoxFor(m => m.RememberMe)%>
<%:Html.LabelFor(m => m.RememberMe)%>
<p>
<input type="submit" value="Log On" />
</p>
<% } %>
</div>
Password Reminder:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<EAZYITT_LOGIN.Models.ReminderModel>"%>
<div id="PasswordReminderDiv">
<h2>PasswordReminder</h2>
<%: Html.ValidationSummary(true) %>
<div class="validation-summary-errors"><span
id="reminderError"></span></div>
<% using (Html.BeginForm("PasswordReminder",
"Account",FormMethod.Post))
{ //'8' o-o +:: %>
<%:Html.LabelFor(m=>m.ReminderEmailAddress) %>
<%:Html.ValidationMessageFor(m =>
m.ReminderEmailAddress)%>
<%:Html.TextBoxFor(m => m.ReminderEmailAddress) %>
<p>
<input type="submit" value="Send Reminder" />
</p>
<%} %>
</div>
The results are submitted to their separate methods in the controller:
[HttpGet]
public ActionResult Login()
{
CombinedViewModel cModel = new CombinedViewModel();
cModel.Logon = new LogOnModel();
cModel.Reminder = new ReminderModel();
return View(cModel);
}
[HttpPost]
public ActionResult Login(CombinedViewModel _login)
{
return View(_login);
}
[HttpGet]
public ActionResult LoginWindow()
{
return PartialView();
}
[HttpPost]
public ActionResult LoginWindow(LogOnModel _login)
{
if (ModelState.IsValid)
{
if (LoginService.ValidateUser(siteId, _login.EmailAddress, _login.Password))
{
//Goto Next Page
ModelState.AddModelError("loginError", "LOGIN - OK");
}
else
{
//Failed Login
ModelState.AddModelError("loginError", "Wrong username or password");
}
}
return PartialView("LoginWindow", _login);
}
[HttpGet]
public ActionResult PasswordReminder()
{
return View();
}
[HttpPost]
public ActionResult PasswordReminder(ReminderModel _reminder)
{
TempData["ModelState"] = ModelState;
if (LoginService.ValidateNewUser(siteId, _reminder.ReminderEmailAddress))
ModelState.AddModelError("reminderError", "The E-mail address does not exist");
if (ModelState.IsValid)
{
ModelState.AddModelError("reminderError", "E-mail found, send e-mail to user");
}
return PartialView("PasswordReminder",_reminder);
}
Ideally, I would like the main view (Login.aspx) to be displayed regardless of the validation.
However, I've currently got the validation on each partial view working, BUT it is taking me to their separate partial views on failed validation as opposed to the main view.
How would I get it to update the partial view only? Is this the way to do it or should I be using AJAX?
The only way to update the partial view only is AJAX, use AJAX.BeginForm or use JQuery. Otherwise, it expects to go through the full lifecycle.
HTH.