I am trying to recreate a project from this textbook called Beginning ASP.NET MVC 4 by Jose Rolando Guay Paz. I am having a little trouble with data validation using data annotations that are part of System.ComponentModel.DataAnnotations.
Here is one of my controller.
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();
}
}
}
and this is the corresponding view for the Send action.
@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")
}
When I run my application and make the request /Message/Send, I get this view.
Now if I click the Send Message button, I should get validation errors like this(according to textbook and also that's what I want to do).
But I am being redirected to ThankYou page, which is basically another action that I have written(You can check my MessageController above). So why is this happening? I followed the textbook exactly and I re-checked my code multiple times, yet I can't figure it out.
P.S : If any further details are needed to solve this, please say so, I will update the details for my question.


class MessageModel, not inclass MessageController