0

This is my Model :

public partial class TAUX
    {
        ....
        [Required(ErrorMessage="Select one At least")]
        public IEnumerable<short> SelectItems { set; get; }
    }

This is my View :

@model pfebs0.Models.TAUX
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="/Scripts/chosen.jquery.js" type="text/javascript"></script>
<link href="/Content/chosen.css" rel="stylesheet"  type="text/css" />
...
            $(".chosen-select").chosen({ width: "100%"})
</script> 
...
            @Html.ListBoxFor(model => model.SelectItems, (ViewBag.CAT_ID as SelectList), new { @class = "chosen-select", data_placeholder = "Selection une Categorie..." })
            @Html.ValidationMessageFor(model => model.SelectItems)

Client Validation dosn't work in my ListBox , but for other Attribute it works fine. How to fix it ?

Chlebta
  • 3,090
  • 15
  • 50
  • 99
  • This question has already been answered [here][1]. [1]: http://stackoverflow.com/questions/6428907/required-attribute-on-generic-list-property – Jason Nesbitt May 14 '14 at 14:47
  • No I want Client Validation – Chlebta May 14 '14 at 14:50
  • You really should have jQuery.validate.unobtrusive in your bundles.. However, do you also have jQuery.Validate.js included somewhere? – Erik Funkenbusch May 14 '14 at 14:57
  • yeah It's included in my parent View and I tried to add it in partial view but always the client side validation not working – Chlebta May 14 '14 at 14:59
  • 2
    Ahh.. Actually, I bet it is... The problem is that you're using chosen.jquery, which actually hides your real listbox, and creates a fake one, which most likely doesn't work with validation. Try not using the chosen and see if it works. – Erik Funkenbusch May 14 '14 at 15:03
  • Yes, you got right, So is there any way to fix that to got both chosen and Validation work together? – Chlebta May 14 '14 at 16:02
  • @ErikFunkenbusch I found this http://stackoverflow.com/questions/20950610/not-getting-validation-message-for-html-dropdownlistfor-once-chosen-jquery-is it helped me but clearing message after validation dosn't worked take look please – Chlebta May 14 '14 at 18:14

1 Answers1

0

@Html.ValidationMessageFor(model => model.CAT_ID)

change to

@Html.ValidationMessageFor(model => model.SelectItems)

Anton
  • 45
  • 4