7

I don't understand why ModelState.isValid give me in all the ways. I set something in the email returns true and I pùt empty field, it returns true too. My question ism, what do I have to do to return true when the field is empty and nothing whn I wrote the email?

I have the next view file:

<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>

The Controller is:

//
// 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 is:

    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; }
    }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Dave
  • 7,028
  • 11
  • 35
  • 58
  • 4
    If you change the signature of your post action from `string email` to `EmailRequest model` and then check the state, what result do you see? – George Johnston Jul 01 '13 at 14:20
  • [HttpPost] public ActionResult EmailRequest(EmailRequest email) { if (ModelState.IsValid) { // save to db, for instance return RedirectToAction("AnotherAction"); } return View(); } – Dave Jul 01 '13 at 14:41
  • The function as you suggested me I changed from string to EmailRequest and it returns null – Dave Jul 01 '13 at 14:42
  • Ok I got the solution for the last comment, I had to add Model in EmailRequest. Now it is working. Thanks. – Dave Jul 01 '13 at 15:52
  • I added my comment as the solution, if it solved your issue. – George Johnston Jul 01 '13 at 16:01
  • @Dave Hi and welcome to SO. To promote users for their effort treating your posts and answering your questions, please vote them up and mark them [as answer](http://i.stack.imgur.com/uqJeW.png). – Shimmy Weitzhandler Jul 14 '13 at 03:05

3 Answers3

7

Change the signature of your post action from string email to EmailRequest model and then check the state. e.g.

[HttpPost]
public ActionResult EmailRequest(EmailRequest model)
{
    if (ModelState.IsValid)
    {
        // save to db, for instance
        return RedirectToAction("AnotherAction");
    }
    return View();
}
George Johnston
  • 31,652
  • 27
  • 127
  • 172
3

You need to bind a view model to your view.

Change your EmailRequest model to something more descriptive like:

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

Your get action method would look something like:

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 view. Please excuse the ASP.NET MVC 4 code, MVC 2 is 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)
}

I hope this helps.

Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
-1

you need to bind your model with binder first to have ability to chek it by Modelstat.IsValid

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