I'm using MVC6 on ASP.NET 5 Beta 8. Trying to use the new tag helpers to submit a single textbox form that captures e-mail with jquery validation.
In my view I've included these scripts in the shared _Layout.cshtml:
<script src="scripts/jquery-1.10.2.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js"></script><script src="http://ajax.aspnetcdn.com/ajax/mvc/5.2.3/jquery.validate.unobtrusive.min.js"></script>
And then my form in the index.cshtml:
<form asp-controller="Home" asp-action="Index" method="post" class="contact-form" name="contact-form"><input id="e-mail" asp-for="Email" placeholder="Enter your e-mail here" class="field-input" /><button class="submit" type="submit" onclick="$(this).resetValidation()">notify me</button><div asp-validation-summary="ValidationSummary.All" class="text-validation"></div></form>
In the controller, I have this code:
// POST: Home/EmailRegister [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Index(EmailRegistration model) { try { if (ModelState.IsValid) { model.CreateEntity = "EmailRegister"; RegistrationContext.EmailRegistrations.Add(model); await RegistrationContext.SaveChangesAsync(); ModelState.AddModelError(string.Empty, "Saved your e-mail!"); } } catch (DbUpdateException) { ModelState.AddModelError(string.Empty, "Duplicate e-mail submitted!" } return View(model); }
What's the best way to detect any errors either model property errors OR errors added to the modelstate in the controller with AddModelError AND change the e-mail input box on the form border red on the client side?