I got an opportunity to work on a custom MVC form to check if user enters the password correct then redirect to other page otherwise show the error, and this was to be done without ajax.
So I created the controller rendering and in the controller created two method as GetPasswordProtectedLogin() and PostPasswordProtectedLogin(Model formData) Which looks like something as following
[HttpGet] public ActionResult GetPasswordProtectedLogin() { if (_botFinder.IsBotOrCrawler) { return this.View("/Views/.../..../ProtectedLoginForm.cshtml", null); } var vm = ProtectedLoginFormViewModel(); vm.IsValid = true; return this.View("/Views/.../.../ProtectedLoginForm.cshtml", vm); } [HttpPost] public ActionResult PostPasswordProtectedLogin(ProtectedLoginFormViewModel formModel) { if (_botFinder.IsBotOrCrawler) { return this.View("/Views/.../.../ProtectedLoginForm.cshtml", null); } var vm = new ProtectedLoginFormViewModel { IsValid = true }; vm = ProtectedLoginFormViewModel(formModel); string password = Sitecore.Globalization.Translate.Text("password"); if (password.Equals(formModel.PasswordValue)) { return this.Redirect(vm.FormSettings.SuccessRedirectionUrl.Url); } vm.IsValid = false; ModelState.AddModelError("Invalid Passcode", vm.FormSettings.PasswordErrorMessage); return this.View("/Views/.../..../ProtectedLoginForm.cshtml", vm); } private rotectedLoginFormViewModel ProtectedLoginFormViewModel(ProtectedLoginFormViewModel formModel = null) { var vm = new ProtectedLoginFormViewModel(); FormSettings datasourceItem; using (new SitecoreContext(Sitecore.Context.Database)) { datasourceItem = GetDataSourceItem<FormSettings>(); } if (datasourceItem == null && Sitecore.Context.Database.Name.Equals(Master)) { datasourceItem = this.GetDatasourceItem_ExperienceEditor<FormSettings>(); } if (datasourceItem == null && formModel != null) { datasourceItem = _sitecoreContext.GetItem<FormSettings>(formModel.Guid); } if (datasourceItem != null) { vm = PasswordProtectedLoginPageViewModelMapper.Map(datasourceItem); } else { return null; } return vm; }
And, the view looks like follows:
@using Glass.Mapper.Sc.Web.Mvc @inherits Glass.Mapper.Sc.Web.Mvc.GlassView<Website.ViewModels.ProtectedLoginFormViewModel> @if (Model?.FormSettings != null) { var isEditModeCSSClass = ""; if (IsInEditingMode) { isEditModeCSSClass = "show-for-editmode"; } using (Html.BeginRouteForm(Sitecore.Mvc.Configuration.MvcSettings.SitecoreRouteName, FormMethod.Post, new { enctype = "multipart/form-data", name = "Default", @class = "protected-login-form" })) { @Html.AntiForgeryToken() @Html.Sitecore().FormHandler("ControllerName", "PostPasswordProtectedLogin")
<div class="blue-forms"> <div class="container"> <div class="row"> <div class="col-sm-12"> @*if (!Model.IsValid || IsInEditingMode) { <div class="section-headline form-content form-thankyou-content"> @Editable(Model.FormSettings, x => x.PasswordErrorMessage) </div> }*@ @Html.ValidationSummary() </div> </div> <div class="row"> <div class="col-sm-6"> <div class="login-form"> @Html.LabelFor(vm => vm.PasswordValue, Model.FormSettings.PasswordLabel) @Html.PasswordFor(vm => vm.PasswordValue, new { @Value = "", Class = "elementfocus", id="passwd", placeholder= Model.FormSettings.PasswordPlaceholder }) @Html.ValidationMessageFor(vm => vm.PasswordValue) @Html.HiddenFor(vm => vm.Guid) </div> </div> <div class="col-sm-12"> @using (BeginEditFrame(Model.FormSettings, null, x => x.ButtonText)) { <button type="submit" id="protectedLoginButton" disabled class="btn btn-light">@Model.FormSettings.ButtonText</button> } </div> </div> </div> </div> } } <script> $("#passwd").on('keyup', function () { let empty = false; empty = $(this).val().length == 0; if (empty) $('#protectedLoginButton').attr('disabled', 'disabled').removeClass("btn-primary").addClass("btn-light"); else $('#protectedLoginButton').attr('disabled', false).removeClass("btn-light").addClass("btn-primary"); }); </script>
Everything works fine but when I was submitting the form then it was giving me the view only instead of the complete layout.
Then I checked this issue was faced by many of the Sitecore guys but I found the root cause of this was due to formHandler as per the StackExchange answer here. So to simply it I did three steps
- Removed the formHandler from the view.
- Renamed my both Get and Post methods to be the same name as follows:
- Updated the method name in the controller rendering and after that when I posted the form it was working fine.
[HttpGet] public ActionResult ProtectedLogin()
[HttpPost] public ActionResult ProtectedLogin(ProtectedLoginFormViewModel formModel)
My Final view was as it is shown above just removed/commented this line completely
@*@Html.Sitecore().FormHandler("ControllerName", "PostPasswordProtectedLogin")*@