MVC 4. ModelState.IsValid always returns true

I do not understand why ModelState.isValid gives me all the ways. I set something in the email, returns true, and I am an empty field, it also returns true. My question ism, what do I need to do to return true when the field is empty, and nothing that I wrote a letter?

I have the following file of the form:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <div style="padding-top:5px;clear:both;"></div> <% using (Html.BeginForm()) { %> <%: Html.ValidationSummary(true) %> <fieldset> <legend>Email usuario</legend> <div class="editor-field"> <%: Html.TextBoxFor(m => m.Email) %> <%: Html.ValidationMessageFor(m => m.Email) %> </div> <input type="submit" value="Enviar Email" /> </fieldset> <% } %> <div style="padding-top:5px;clear:both;"></div> </asp:Content> 

Controller:

 // // GET: /Account/EmailRequest public ActionResult EmailRequest() { return View(); } [HttpPost] public ActionResult EmailRequest(string email) { if (ModelState.IsValid) { // save to db, for instance return RedirectToAction("AnotherAction"); } return View(); } 

My model class:

  using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Globalization; using System.Web.Mvc; using System.Web.Security; namespace PortalClient.Models { public class EmailRequest { [Required(ErrorMessage = "required")] public string Email { get; set; } } } 
+6
source share
3 answers

Change the signature of your action from string email to EmailRequest model , and then check the status. eg.

 [HttpPost] public ActionResult EmailRequest(EmailRequest model) { if (ModelState.IsValid) { // save to db, for instance return RedirectToAction("AnotherAction"); } return View(); } 
+6
source

You need to bind the view model to your view.

Change your EmailRequest model to something more descriptive, for example:

 public class EmailRequestViewModel { [Required(ErrorMessage = "Required")] public string Email { get; set; } } 

Your action method will look something like this:

 public ActionResult EmailRequest() { EmailRequestViewModel viewModel = new EmailRequestViewModel(); return View(viewModel); } 

Your post action method:

 public ActionResult EmailRequest(EmailRequestViewModel viewModel) { // Check for null view model if (!ModelState.IsValid) { return View(viewModel); } // Do whatever you need to do return RedirectToAction("List"); } 

And then your opinion. Sorry ASP.NET MVC 4 code, MVC 2 prehistoric :) This is just part of your view:

 @model YourProject.ViewModels.EmailRequestViewModel @using (Html.BeginForm()) { @Html.TextBoxFor(x => x.Email) @Html.ValidationMessageFor(x => x.Email) } 

Hope this helps.

+3
source

you need to link your model with a binder first to be able to tickle it with Modelstat.IsValid

  public ActionResult EmailRequest() { EmailRequest email = new EmailRequest(); TryUpdateModel(email); if (ModelState.IsValid) { // save to db, for instance return RedirectToAction("AnotherAction"); } return View(); } 
-1
source

Source: https://habr.com/ru/post/948479/


All Articles