我正试图通过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();
}
}
}
这是Send动作的相应视图。
@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")
}
当我运行我的应用程序并发出请求/消息/发送时,我得到了这个视图。
现在,如果我点击发送消息按钮,我应该得到这样的验证错误(根据教科书以及我想做的事情)。
但是我被重定向到ThankYou
页面,这基本上是我写的另一个动作(你可以检查我上面的MessageController)。那为什么会这样呢?我完全按照教科书,多次重新检查我的代码,但我无法弄明白。
P.S:如果需要进一步的细节来解决这个问题,请说明,我会更新我的问题的详细信息。
答案 0 :(得分:1)
可能你误读了样本。
您在MessageController
中列出的属性应在模型类中定义 - HaveYouSeenMe.Models.MessageModel
- 而不是在控制器中。可能他们已经在那里定义,但注释不是。
您需要做的是将数据注释从控制器移动到模型类。然后你可以摆脱控制器中的属性,只留下最后两种方法。