0

Ma validation de modèle n'affiche pas les messages d'erreur dans le navigateur client. Le formulaire est soumis mais dans mon contrôleur, mon ModelState.IsValid est faux avec les messages d'erreur appropriés dans mon objet ModelState. Je ne suis pas sûr de savoir comment corriger ce qui ne va pas.asp.net MVC4 validation du modèle côté client

My model class : 
Imports System.ComponentModel.DataAnnotations 
Public Class RateBO 
<Required(AllowEmptyStrings:=False, ErrorMessage:="Quantity for Miles is required!")> 
Public Property MilesQty As Decimal 
Public Property MilesAmt As Decimal 

<Required(AllowEmptyStrings:=False, ErrorMessage:="Quantity for Service is required!")> 
Public Property FirstServiceQty As Integer 
Public Property FirstServiceAmt As Integer 
Public Property SubServiceQty As Integer 
Public Property SubServiceAmt As Decimal 
Public Property TotalRate As Decimal 
End Class 

My Viewmodel class : 
Public Class FeeEntryVM 
Public Property FeeRate As UnitPriceBO 
Public Property UIFee As RateBO 
End Class 

My View : 
@ModelType JUD.ITD.Portal.SSO.JudPortal.MarshalOrderRegistry.RateEntryVM 

@Code 
ViewData("Title") = "GetRateView" 
Layout = "~/Views/Shared/_Layout.vbhtml" 
End Code 
@Using (Html.BeginForm("SubmitRate", "OrderRegistryService", FormMethod.Post, New With  {.id = "RateEntryForm"})) 
@<text> 
<div class='container-fluid span12' style="margin-left:-5%"> 
<table class="table"> 
<thead> 
    <tr> 
     <th class="span4 mobLabel" style="text-align:center">Rate Description</th> 
     <th class="span2 mobLabel" style="text-align:center">Quantity</th> 
     <th class="span1 mobLabel" style="text-align:center">Unit Price($)</th> 
     <th class="span1 mobLabel" style="text-align:center">Amount($)</th> 
    </tr> 
</thead> 
<tbody> 
    <tr> 
     <td class="span4" style="text-align:center">Service</td> 
     <td class="span2"> 
      @Html.TextBoxFor(Function(model) model.UIRate.FirstServiceQty, New With {.class = "Ratehalf", .type = "text", .id = "serviceQty"}) 
      @Html.ValidationMessageFor(Function(model) model.UIRate.FirstServiceQty) 
     </td> 
     <td class="span1"> 
      @Html.TextBoxFor(Function(model) model.RateRate.FirstServiceRate, New With {.class = "Ratehalf", .readOnly = True, .type = "text", .id = "serviceRate"}) 
     </td> 
     <td class="span1"> 
      @Html.TextBoxFor(Function(model) model.UIRate.FirstServiceAmt, New With {.class = "Ratehalf", .readOnly = True, .type = "text", .id = "serviceAmt"}) 
      @Html.ValidationMessageFor(Function(model) model.UIRate.FirstServiceAmt) 
     </td> 
    </tr> 
    <tr> 
     <td class="span4" style="text-align:center"> Subsequent Services</td> 
     <td class="span2"> 
      @Html.TextBoxFor(Function(model) model.UIRate.SubServiceQty, New With {.class = "Ratehalf", .type = "text", .id = "subServiceQty"}) 
      @Html.ValidationMessageFor(Function(model) model.UIRate.SubServiceQty) 
     </td> 
     <td class="span1"> 
      @Html.TextBoxFor(Function(model) model.RateRate.SubServiceRate, New With {.class = "Ratehalf", .readOnly = True, .type = "text", .id = "subServiceRate"}) 
     </td> 
     <td class="span1"> 
      @Html.TextBoxFor(Function(model) model.UIRate.SubServiceAmt, New With {.class = "Ratehalf", .readOnly = True, .type = "text", .id = "SubServiceAmt"}) 
      @Html.ValidationMessageFor(Function(model) model.UIRate.SubServiceAmt) 
     </td> 
    </tr> 
    <tr> 
     <td class="span4" style="text-align:center">Miles</td> 
     <td class="span2"> 
      @Html.TextBoxFor(Function(model) model.UIRate.MilesQty, New With {.class = "Ratehalf", .type = "text", .id = "milesQty"}) 
      @Html.ValidationMessageFor(Function(model) model.UIRate.MilesQty) 
     </td> 
     <td class="span1"> 
      @Html.TextBoxFor(Function(model) model.RateRate.MilesRate, New With {.class = "Ratehalf", .readOnly = True, .type = "text", .id = "milesRate"}) 
     </td> 
     <td class="span1"> 
      @Html.TextBoxFor(Function(model) model.UIRate.MilesAmt, New With {.class = "Ratehalf", .readOnly = True, .type = "text", .id = "milesAmt"}) 
      @Html.ValidationMessageFor(Function(model) model.UIRate.MilesAmt) 
     </td> 
    </tr> 
    <tr> 
     <td class="span4" style="text-align:center"></td> 
     <td class="span2"></td> 
     <td class="span1" style="text-align:center;"><b>Total Rate($)</b></td> 
     <td class="span1"> 
      @Html.TextBoxFor(Function(model) model.UIRate.TotalRate, New With {.class = "Ratehalf", .readOnly = True, .type = "text", .id = "totalRate"}) 
      @Html.ValidationMessageFor(Function(model) model.UIRate.TotalRate) 
     </td> 
     <td></td> 
    </tr> 
</tbody> 
</table> 
</div> 
<div class="row offDisplay"> 
<div class="rowBig"> 
    <div class="span12"> 
     <button class="half btn btn-primary span2 text-center" id="btnSubmit" type="submit">Submit</button> 
     <button class="half offset1 btn btn-primary span2 text-center" id="btnCancel" type="reset">Cancel</button> 
    </div> 
</div> 
</div> 
</text> 
End Using 

Répondre

1

Votre question semble un peu trompeur, mais je pense que vous demandez pourquoi la validation client ne fonctionne pas. Assurez-vous que vous avez ces deux scripts sur votre page:

~/Scripts/jquery.unobtrusive.min.js 

~/Scripts/jquery.validate.min.js 

je les mets habituellement dans un paquet dans les BundleConfig.cs

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
        "~/Scripts/jquery.unobtrusive*", 
        "~/Scripts/jquery.validate*")); 

En utilisant l'astérisque * au lieu de min.js le Bundler utilisera la version non minified en développement et utilisera la version minifiée en production.

Puis dans ma page de mise en page:

@Scripts.Render("~/bundles/jqueryval") 
+0

Merci Nick, j'ai raté ça! – user2721870

-1

Utilisez ModelState.IsValid() et donc attraper l'exception qui va vous expliquer ce qui se passait mal avec le ModelState

+0

J'utilise que sur mon contrôleur et est « Faux » avec le message d'erreur correspondant pour mes propriétés du modèle. Mais je ne comprends pas pourquoi mon formulaire est soumis lorsque les validations échouent. Et les messages n'apparaissent pas dans la page de mon navigateur. – user2721870

+0

Il a déjà dit qu'il sait pourquoi son modèle est invalide, il demande pourquoi la validation ne se fait pas côté client (JavaScript). –

+0

Mon mauvais, n'a pas lu la question correctement. Pardonnez-moi :( –

Questions connexes