【问题标题】:Why is Asp.net validation using data annotations not working?为什么使用数据注释的 Asp.net 验证不起作用?
【发布时间】:2016-07-21 09:56:02
【问题描述】:

我正在尝试从 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 : 如果需要任何进一步的细节来解决这个问题,请说出来,我会更新我的问题的细节。

【问题讨论】:

  • 首先检查生成的html是否有“data-”验证属性。
  • 数据注解应该在模型类上,而不是在控制器上。
  • 我假设您发布了一些不正确的代码。这些属性应该在class MessageModel,而不是class MessageController
  • 哎呀,太尴尬了。对我来说这是一个愚蠢的错误。非常感谢。

标签: c# asp.net asp.net-mvc validation data-annotations


【解决方案1】:

您可能误读了示例。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    相关资源
    最近更新 更多