Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValidationMessageFor not showing errors

Tags:

asp.net-mvc

I have this class:

public class Product
{
    [Required(ErrorMessage = "empty name")]
    public string Name { get; set; }

    [Required(ErrorMessage = "empty description")]
    public string Description { get; set; }
}

and I have a ResponseProduct which is shown in view:

public class ProductInsertVM
{
    public string Message = "Success";
    public Product Product;
}

In my view I have this:

@using (Html.BeginForm()){  
    @Html.ValidationSummary(false)

    @Html.TextBoxFor(m => m.Product.Description)
    @Html.ValidationMessageFor(m => m.Product.Description)
}

What I want to know is why ValidationMessageFor doest'n work? !! only ValidationSummary works. If I respond with a Product, so it works.

like image 336
MuriloKunze Avatar asked May 21 '12 21:05

MuriloKunze


1 Answers

The code you provided should work fine if the following conditions are met.

1) You have reference to the jQuery validation scripts in your view

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

2) On the form submit, you are calling the ModelState.IsValid property and hence validating the model passed to the action method

[HttpPost]
public ActionResult CarList(CarList model)
{
    if (ModelState.IsValid)
    {
       //Save or whatever you want to do 
    }
    return View(model);
}
like image 119
Shyju Avatar answered Oct 07 '22 02:10

Shyju