7

我不明白为什么 ModelState.isValid 会以各种方式给我。我在电子邮件中设置了一些返回 true 的内容,我输入了空字段,它也返回 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; }
    }
}
4

3 回答 3

7

将您的发布操作的签名从更改string emailEmailRequest model,然后检查状态。例如

[HttpPost]
public ActionResult EmailRequest(EmailRequest model)
{
    if (ModelState.IsValid)
    {
        // save to db, for instance
        return RedirectToAction("AnotherAction");
    }
    return View();
}
于 2013-07-01T16:01:17.630 回答
3

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

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

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

您的 get 操作方法如下所示:

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

我希望这有帮助。

于 2013-07-02T09:34:18.103 回答
-1

您需要先将模型与活页夹绑定,才能通过 Modelstat.IsValid 对其进行检查

    public ActionResult EmailRequest()
    {
           EmailRequest email = new EmailRequest();
           TryUpdateModel(email);
        if (ModelState.IsValid)
        {
            // save to db, for instance
            return RedirectToAction("AnotherAction");
        }
        return View();
    }
于 2014-08-08T09:45:56.767 回答