0

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.

enter image description here

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

enter image description here

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.

4
  • First of all, check if generated html has "data-" validation attributes. Commented Jul 21, 2016 at 9:57
  • 1
    The data annotation should be on the model class and not on the controller. Commented Jul 21, 2016 at 9:57
  • 1
    I assume you have posted some incorrect code. Those properties should be in class MessageModel, not in class MessageController Commented Jul 21, 2016 at 9:59
  • Oops, that's so embarrassing. Such a silly mistake on my end. Thanks a lot. Commented Jul 21, 2016 at 10:18

1 Answer 1

1

Probably you misread the sample.

The properties you've listed in the MessageController should be defined in the model class - HaveYouSeenMe.Models.MessageModel - not in the controller. And probably they are already defined there but the annotations are not.

What you need to do is to move the data annotations from the controller to the model class. Then you can get rid of the properties in the controller, leaving just the last two methods.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.