1

I am newer to using MVC. I am trying to set up client-side validation using the build in data annotations. I have read a few tutorials but can't seem to get it to work. When I click my form submit button, it still POSTS instead of displaying the error message to inform me no description was provided. Below is my class code and my view code:

Class Code:

using System.ComponentModel.DataAnnotations;
namespace TicketSystem.Models
{
using System;
using System.Collections.Generic;

public partial class TICKET
{
    public decimal id { get; set; }
    public string empId { get; set; }
    public short severityId { get; set; }
    public short statusId { get; set; }
    public short categoryId { get; set; }

    [Required(ErrorMessage = "Description is required!!")]
    public string description { get; set; }
    public string logOfActions { get; set; }
    public string deviceType { get; set; }
    public string deviceSerNum { get; set; }
    public System.DateTime dateCreated { get; set; }
 }
}

View Code:

@Html.LabelFor(m => m.description, "Description:")
@Html.EditorFor(m => m.description, new { rows = 5, @class = "txtBoxDescr" })
@Html.ValidationMessageFor(m => m.description)
Stc5097
  • 291
  • 1
  • 11
  • 25
  • Can you show more of the view code? How are you constructing the form? – DavidG Sep 30 '14 at 14:29
  • possible duplicate of [MVC 4 client side validation not working](http://stackoverflow.com/questions/14520336/mvc-4-client-side-validation-not-working) – CodeCaster Sep 30 '14 at 14:32
  • Two most frequent issue to check: 1.Make sure you enabled unobstrusive validation in web.config 2.Make sure your page have the following javascript referenced and in right order: jquery, jquery.validate, jquery.validate.unobstrusive – tweray Sep 30 '14 at 14:37
  • I have the values enabled in web.config and the scrips are rendered in that order in my _Layout.cshtml but still doesn't work – Stc5097 Sep 30 '14 at 14:57
  • Any JS errors on page? An errant piece of JS code can break all further JS on the page. – Chris Pratt Sep 30 '14 at 16:09

1 Answers1

0

In your Controller, be sure to use the following code:

if ModelState.IsValid()
{
  //Do Something here
   return RedirectToAction("Home");
}

return View(myModel);

You can check a better explanation here.

Community
  • 1
  • 1
Maturano
  • 951
  • 4
  • 15
  • 42