views:

85

answers:

1
[MetadataType(typeof(Deal_Validation))]
    public partial class Deal
    {
    }

    public class Deal_Validation
    {
        [Required]
        public string Title { get; set; }

        public double? EstValue { set; get; }
    }

How to validate EstValue (check if it is of type double?)

Thanks

A: 

Please look here: http://stackoverflow.com/questions/2936529/validating-primitive-types-in-asp-net-mvc/2936826#2936826

tpeczek
I read the blog you linked, but I see no solution for my problem!?
ile
Read the "Input Validation" part carefully. You will find there an information that validation of data being compatible with the destination type is performed automatic by ModelBinder (so you don't have to validate if its double, it will double or null).
tpeczek
I understand that, but how to display error using Html.ValidationMessageFor()? Or I will have to use other way?
ile
The error will be displayed automaticly if you put Html.ValidationMessageFor() (ModelBinder will add the error into the ModelState so it will be rendered, the message will be "The value 'EstValue' is not valid.")
tpeczek
You are right... I used hardcoded html for this field, that's why I didn't get any error messages. Now I have other problem, when I click on submit button then error is returned after page is submitted, client-side validation doesn't work for this field, while it works for other fields. Any idea why is that so?
ile
Client-side works only if required attribute is enabled, but type-validation works only when page is submitted.
ile
If you want to perform type-validation on client side, you will have to write your own validator for this. Please look at those links: http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx, http://tpeczek.blogspot.com/2010/04/unobtrusive-asynchronous-form-in-aspnet.html
tpeczek