Model
Validation Class
Registration in Global.asax
Controller
View
Problem
Client side validation works perfectly. No issue here. However, I've tried to make sure that the validation will work even when the JS is disabled on the browser. What I've expected was to see the error messages after page reload when I submit the form, without client side validations.
However, the result is - no validation messages at all, but only query string params in the URL with submitted values.
Question
How can I make validation work when JavaScript disabled? What I need is (when validation fails) showing the validation messages after the post and page reload.
publicclass UserViewModel { public String Email { get; set; } public String PhoneNumber { get; set; } public String FullName { get; set; } public String CompanyName { get; set; } }
publicclass UserViewModelValidation: AbstractValidator<UserViewModel> { public UserViewModelValidation() { RuleFor(x => x.Email).NotNull().WithLocalizedMessage(() => ValidationResources.EmailRequired); RuleFor(x => x.Email).EmailAddress().WithLocalizedMessage(() => ValidationResources.EmailValid); RuleFor(x => x.PhoneNumber).NotNull().WithLocalizedMessage(() => ValidationResources.PhoneNumberRequired); RuleFor(x => x.FullName).NotNull().WithLocalizedMessage(() => ValidationResources.FullNameRequired); RuleFor(x => x.CompanyName).NotNull().WithLocalizedMessage(() => ValidationResources.CompanyNameRequired); } }
FluentValidationModelValidatorProvider.Configure();
[HttpPost] public ActionResult UpdateUser(UserViewModel vm) { if (!ModelState.IsValid) return View(vm); var service = DiConfig.Resolve<IUserService>(); service.Update(vm); return RedirectToAction("Index"); }
<formaction="@Url.Action("UpdateUser", "User")"> @Html.ValidationSummary(true) <divclass="form-input"> @Html.LabelFor(m => m.Email, FormResources.Email) @Html.TextBoxFor(m => m.Email) <divclass="error-message">@Html.ValidationMessageFor(m => m.Email)</div></div><divclass="form-input"> @Html.LabelFor(m => m.PhoneNumber, FormResources.PhoneNumber) @Html.TextBoxFor(m => m.PhoneNumber) <divclass="error-message">@Html.ValidationMessageFor(m => m.PhoneNumber)</div></div><divclass="form-input"> @Html.LabelFor(m => m.FullName, FormResources.FullName) @Html.TextBoxFor(m => m.FullName) <divclass="error-message">@Html.ValidationMessageFor(m => m.FullName)</div></div><divclass="form-input"> @Html.LabelFor(m => m.CompanyName, FormResources.CompanyName) @Html.TextBoxFor(m => m.CompanyName) <divclass="error-message">@Html.ValidationMessageFor(m => m.CompanyName)</div></div><divclass="form-input"><inputtype="submit"value="@FormResources.UpdateButton"></div></form>
Client side validation works perfectly. No issue here. However, I've tried to make sure that the validation will work even when the JS is disabled on the browser. What I've expected was to see the error messages after page reload when I submit the form, without client side validations.
However, the result is - no validation messages at all, but only query string params in the URL with submitted values.
Question
How can I make validation work when JavaScript disabled? What I need is (when validation fails) showing the validation messages after the post and page reload.