Mam RangeValidator
na właściwości w moim modelu, aby zezwalać tylko na wartości całkowite od 0 do 100. Mam częściowy widok, który wyświetla formularz aktualizujący właściwość poprzez okno dialogowe interfejsu użytkownika jQuery . Przyjrzałem się źródłu i mogę potwierdzić, że atrybuty adnotacji danych są generowane poprawnie. Jednak walidacja nie działa poprawnie. Wykonuje jakąś walidację, ale nie korzysta z ustawionego zakresu. Wartości 1, 10 i 100 nie powodują błędu. Każda inna pojedyncza lub dwucyfrowa wartość powoduje błąd. Jeśli jednak wypełniam zera, wszystkie wartości mniejsze niż sto są w porządku.Sprawdzanie poprawności zakresów danych MVC nie działa poprawnie
Model:
public class MyModel
{
...
[Required(ErrorMessage = "{0} is required")]
[Range(typeof(int), "0", "100",
ErrorMessage = "{0} can only be between {1} and {2}")]
public int Percentage { get; set; }
...
}
Częściowy Widok:
@model MyApp.Models.Partials.MyModel
<div id="myDialog" class="editModal" title="Update Percentage">
@using (Ajax.BeginForm("UpdatePercentage", "MyController",
new AjaxOptions() { HttpMethod = "Post",
OnSuccess = "onUpdateSuccess" },
new { name = "myForm" }))
{
@Html.ValidationSummary(null, new { style = "width:auto;max-width:22em;
float:left;clear:both;" })
<div style="width:auto;float:left;clear:both;">
<div style="width:10em;float: left;text-align:right;clear:left;">
@Html.Label("Percentage:")
</div>
<div style="width:12em;margin-left:1em;float:left;clear:right;">
@Html.TextBoxFor(m => m.Percentage)
</div>
</div>
<div style="width:23em;clear:both;text-align:center;">
<hr />
<input type="button" value="Cancel" class="cancelModalButton"
data-modal="myDialog" />
<input type="submit" value="Submit" class="submitModalButton"
data-modal="myDialog" data-form="myForm" />
</div>
}
</div>
Markup produkowane:
<div id="myDialog" class="editModal" title="Update Percentage">
<form action="/Web/MyController/UpdatePercentage?Length=3"
data-ajax="true" data-ajax-method="Post" data-ajax-success="onUpdateSuccess"
id="form2" method="post" name="myForm">
<div class="validation-summary-valid" data-valmsg-summary="true"
style="width:auto;max-width:22em;float:left;clear:both;">
<ul>
<li style="display:none"></li>
</ul>
</div>
<div style="width:auto;float:left;clear:both;margin-bottom:.75em;">
<div style="width:10em;float: left;text-align:right;clear:left;">
<label for="Percentage:">Percentage:</label>
</div>
<div style="width:12em;margin-left:1em;float:left;clear:right;">
<input data-val="true" data-val-number="The field Percentage must be a number."
data-val-range="Percentage can only be between 0 and 100"
data-val-range-max="100" data-val-range-min="0"
data-val-required="Percentage is required" id="Percentage"
name="Percentage" type="text" value="10" />
</div>
</div>
<div style="width:23em;clear:both;text-align:center;">
<hr />
<input type="button" value="Cancel" class="cancelModalButton" data-modal="myDialog" />
<input type="submit" value="Submit" class="submitModalButton" data-modal="myDialog" data-form="myForm" />
</div>
</form>
</div>
widzę, że typ jest ustawiony na text
, a jeśli zmienię TextBoxFor
do EditorFor
to chnages do number
, ale wciąż zobacz to samo zachowanie.
jest to problem z łamaniem zmian w jquery, zobacz: http://stackoverflow.com/questions/14889431/client-side-validation-trips-on-dataannotation -range-atrybut – felickz