Bonjour,

je rencontre un problème sur une liste d'input (ma liste de mois).
J'ai un formulaire pour ajouter un nombre d'heures par mois sur une année. La personne choisit l'année et remplit chaque mois.
Lors de la validation du formulaire, au niveau du modele, j'aimerais vérifier que chaque case de ma liste est bien supérieure à 0 et inférieure à 1000 et dans le cas contraire, envoyer une erreur sur le champs en question (astérisque par exemple)

J'ai essayé le Range mais cela met une erreur au modelState sur la liste même si tout est OK.

Vue :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
 
<h2>
        Actions</h2>
    <% using (Html.BeginForm("ListerHeuresTravaillees", "Calendrier", FormMethod.Post))
       { %>
    <fieldset class="fldstFormulaire">
        <legend>Actions</legend>
        <table>
            <tr>
                <td>
                    Compte
                </td>
                <td>
                    <% IList<itsges.Models.CompteModele> l = (IList<itsges.Models.CompteModele>)itsges.Models.CompteModele.ListeCompte();%>
                    <% l.Insert(0, null); %>
                    <%: Html.DropDownList("CodeCompte", new SelectList(l, "Code", "SSSystemeTiretNom"))%>
                    <%: Html.ValidationMessageFor(m => m.CodeCompte)%>
                </td>
            </tr>
            <tr>
                <td>
                    Année
                </td>
                <td>
                    <%: Html.TextBoxFor(m => m.Annee, new { style = "width:40px;", maxlength="4"})%>
                    <%: Html.ValidationMessageFor(m => m.Annee)%>
                </td>
            </tr>
            <tr>
                <td>
                    Janvier
                </td>
                <td>
                    <%: Html.TextBoxFor(m => m.ListeH[0], new { style = "width:70px;", maxlength = "6" })%>
                    <%: Html.ValidationMessageFor(m => m.ListeH[0])%>
                </td>
            </tr>
            <tr>
                <td>
                    Février
                </td>
                <td>
                    <%: Html.TextBoxFor(m=>m.ListeH[1], new { style = "width:70px;", maxlength = "6" }) %>
                    <%: Html.ValidationMessageFor(m => m.ListeH[1])%>
                </td>
            </tr>
            <tr>
                <td>
                    Mars
                </td>
                <td>
                    <%: Html.TextBoxFor(m=>m.ListeH[2], new { style = "width:70px;", maxlength = "6" }) %>
                    <%: Html.ValidationMessageFor(m => m.ListeH[2])%>
                </td>
            </tr>
            <tr>
                <td>
                    Avril
                </td>
                <td>
                    <%: Html.TextBoxFor(m => m.ListeH[3], new { style = "width:70px;", maxlength = "6" })%>
                    <%: Html.ValidationMessageFor(m => m.ListeH[3])%>
                </td>
            </tr>
            <tr>
                <td>
                    Mai
                </td>
                <td>
                    <%: Html.TextBoxFor(m => m.ListeH[4], new { style = "width:70px;", maxlength = "6" })%>
                    <%: Html.ValidationMessageFor(m => m.ListeH[4])%>
                </td>
            </tr>
            <tr>
                <td>
                    Juin
                </td>
                <td>
                    <%: Html.TextBoxFor(m => m.ListeH[5], new { style = "width:70px;", maxlength = "6" })%>
                    <%: Html.ValidationMessageFor(m => m.ListeH[5])%>
                </td>
            </tr>
            <tr>
                <td>
                    Juillet
                </td>
                <td>
                    <%: Html.TextBoxFor(m => m.ListeH[6], new { style = "width:70px;", maxlength = "6" })%>
                    <%: Html.ValidationMessageFor(m => m.ListeH[6])%>
                </td>
            </tr>
            <tr>
                <td>
                    Août
                </td>
                <td>
                    <%: Html.TextBoxFor(m => m.ListeH[7], new { style = "width:70px;", maxlength = "6" })%>
                    <%: Html.ValidationMessageFor(m => m.ListeH[7])%>
                </td>
            </tr>
            <tr>
                <td>
                    Septembre
                </td>
                <td>
                    <%: Html.TextBoxFor(m => m.ListeH[8], new { style = "width:70px;", maxlength = "6" })%>
                    <%: Html.ValidationMessageFor(m => m.ListeH[8])%>
                </td>
            </tr>
            <tr>
                <td>
                    Octobre
                </td>
                <td>
                    <%: Html.TextBoxFor(m => m.ListeH[9], new { style = "width:70px;", maxlength = "6" })%>
                    <%: Html.ValidationMessageFor(m => m.ListeH[9])%>
                </td>
            </tr>
            <tr>
                <td>
                    Novembre
                </td>
                <td>
                    <%: Html.TextBoxFor(m => m.ListeH[10], new { style = "width:70px;", maxlength = "6" })%>
                    <%: Html.ValidationMessageFor(m => m.ListeH[10])%>
                </td>
            </tr>
            <tr>
                <td>
                    Décembre
                </td>
                <td>
                    <%: Html.TextBoxFor(m => m.ListeH[11], new { style = "width:70px;", maxlength = "6" })%>
                    <%: Html.ValidationMessageFor(m => m.ListeH[11])%>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <input type="submit" name="submitButton" value="Ajouter" />
                    <input type="submit" name="submitButton" value="Modifier" />
                    <input type="submit" name="submitButton" value="Supprimer" />
                </td>
            </tr>
        </table>
    </fieldset>
    <%} %>
Controller,
(ListeH)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
public ActionResult ListerHeuresTravaillees()
        {
            return View("ListerHeuresTravaillees");
        }
 
        [HttpPost]
        public ActionResult ListerHeuresTravaillees(string submitButton, HeuresTravailleesModele h)
        {
            if (ModelState.IsValid)
            {
                switch (submitButton)
                {
                    case "Ajouter":
                        h.Insert();
                        break;
 
                    case "Modifier":
                        ViewBag.message = "///TODO modification ";
                        break;
                    case "Supprimer":
                        ViewBag.message = "/// todo supprimer";
                        break;
                    default:
                        break;
                }
                return RedirectToAction("ListerHeuresTravaillees");
            }
            else return ListerHeuresTravaillees();
Modele
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
 
public class HeuresTravailleesModele
    {
        [Required(ErrorMessage = "*")]
        public string CodeCompte { get; set; }
 
        [Required(ErrorMessage = "*")]
        public int Annee { get; set; }
 
        [Required(ErrorMessage = "*")]
        //[Range(0, 1000, ErrorMessage = "Heure incorrecte")]
        public IList<double> ListeH { get; set; }
}
Je rencontre ce problème dans deux formulaires donc j'aimerais trouvé une solution. (La solution de remplacer la liste par X champs validables ne convient pas dans l'autre cas et ne me plait pas)

Merci par avance.

@+