为什么 Asp.net 使用数据注释的验证不起作用?

Why is Asp.net validation using data annotations not working?

我正在尝试根据 Jose Rolando Guay Paz 的这本名为 Beginning ASP.NET MVC 4 的教科书重新创建一个项目。我在使用属于 System.ComponentModel.DataAnnotations.

的数据注释进行数据验证时遇到了一些麻烦

这是我的控制器之一。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HaveYouSeenMe.Models;
using System.ComponentModel.DataAnnotations;

namespace HaveYouSeenMe.Controllers
{
    public class MessageController : Controller
    {
        //
        // GET: /Message/

        [Required(ErrorMessage = "Please type your name")]
        [StringLength(150, ErrorMessage = "You can only add upto 150 characters")]
        [FullName(ErrorMessage = "Please type your full name")]
        public string From { get; set; }

        [Required(ErrorMessage = "Please type your email address")]
        [StringLength(150, ErrorMessage = "You can only add upto 150 characters")]
        [EmailAddress(ErrorMessage = "We don't recognize this as a valid email address")]
        public string Email { get; set; }

        [StringLength(150, ErrorMessage = "You can only add upto 150 characters")]
        public string Subject { get; set; }

        [Required(ErrorMessage = "Please type your message")]
        [StringLength(1500, ErrorMessage = "You can only add upto 1500 characters")]
        public string Message { get; set; }

        public ActionResult Send()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Send(MessageModel model)
        {
            if (ModelState.IsValid)
            { 
                return RedirectToAction("ThankYou");
            }
            ModelState.AddModelError("", "One or more errors were found");
            return View(model);
        }

        public ActionResult ThankYou()
        {
            return View();
        }
    }
}

这是发送操作的对应视图。

@model HaveYouSeenMe.Models.MessageModel
@{
ViewBag.Title = "Send";
}
<h2>Send Message</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>MessageModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.From)

</div>
    <div class="editor-field">
@Html.EditorFor(model => model.From)
@Html.ValidationMessageFor(model => model.From)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Subject)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Subject)
@Html.ValidationMessageFor(model => model.Subject)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Message)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Message)
@Html.ValidationMessageFor(model => model.Message)
</div>
<p>
<input type="submit" value="Send Message" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

当我 运行 我的应用程序并提出请求 /Message/Send 时,我得到了这个视图。

现在,如果我单击“发送消息”按钮,我应该会收到这样的验证错误(根据教科书,这也是我想要做的)。

但我被重定向到 ThankYou 页面,这基本上是我编写的另一个操作(您可以查看上面的 MessageController)。那么为什么会这样呢?我完全按照教科书做的,我多次重新检查我的代码,但我还是想不通。

P.S : 如果需要任何进一步的细节来解决这个问题,请说出来,我会更新我的问题的细节。

你可能误读了样本。

您在 MessageController 中列出的属性应该在模型 class - HaveYouSeenMe.Models.MessageModel 中定义 - 而不是在控制器中。可能它们已经在那里定义了,但注释还没有。

您需要做的是将数据注释从控制器移动到模型class。然后就可以去掉controller中的属性,只留下最后两个方法