MVC 4. ModelState.IsValid始终返回true

时间:2013-07-01 14:12:26

标签: c# asp.net-mvc asp.net-mvc-4 modelstate

我不明白为什么ModelState.isValid会以各种方式给我。我在电子邮件中设置了一些返回true并且我在空字段中输入,它也返回true。我的问题是,当该字段为空且我没有写过电子邮件时,我该怎么做才能返回?

我有下一个视图文件:

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

控制器是:

//
// 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();
}

我的模特课是:

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

3 个答案:

答案 0 :(得分:6)

将帖子操作的签名从string email更改为EmailRequest model,然后检查状态。 e.g。

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

答案 1 :(得分:3)

您需要将视图模型绑定到视图。

将您的EmailRequest模型更改为更具描述性的内容:

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

您的获取操作方法如下所示:

public ActionResult EmailRequest()
{
     EmailRequestViewModel viewModel = new EmailRequestViewModel();

     return View(viewModel);
}

您的帖子操作方法:

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");
}

然后你的观点。请原谅ASP.NET MVC 4代码,MVC 2是史前的:)这只是你观点的一部分:

@model YourProject.ViewModels.EmailRequestViewModel

@using (Html.BeginForm())
{
     @Html.TextBoxFor(x => x.Email)
     @Html.ValidationMessageFor(x => x.Email)
}

我希望这会有所帮助。

答案 2 :(得分:-1)

您需要首先将模型与binder绑定,以便能够通过Modelstate.IsValid检查它

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