【问题标题】:asp.net MVC4 client side model validationasp.net MVC4 客户端模型验证
【发布时间】:2023-04-01 16:22:01
【问题描述】:

我的模型验证未在客户端浏览器中显示错误消息。表单已提交,但在我的控制器中,我的 ModelState.IsValid 为 false,并且在我的 ModelState 对象中显示了相应的错误消息。我不确定如何调试出了什么问题。

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

【问题讨论】:

    标签: vb.net asp.net-mvc-4 jquery-validate unobtrusive-validation


    【解决方案1】:

    您的问题似乎有点误导,但我认为您是在问为什么客户端验证不起作用。确保您的页面上有这两个脚本:

    ~/Scripts/jquery.unobtrusive.min.js
    
    ~/Scripts/jquery.validate.min.js
    

    我通常将它们打包在BundleConfig.cs

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

    通过使用星号 * 而不是 min.js,捆绑器将在开发中使用非缩小版本,并将在生产中使用缩小版本。

    然后在我的布局页面中:

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

    【讨论】:

    • 谢谢尼克。我不知何故错过了这个!
    【解决方案2】:

    使用ModelState.IsValid() so 并捕获异常以解释 ModelState 出了什么问题

    【讨论】:

    • 我在我的控制器上使用它并且是 'False' 和我的模型属性的相应错误消息。但我不明白为什么当验证失败时我的表单会被提交。而且这些消息也没有显示在我的浏览器页面中。
    • 他已经说过他知道为什么他的模型无效,他在问为什么验证不会发生在客户端(JavaScript)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 2013-11-24
    • 2012-05-05
    • 2015-11-08
    • 2010-09-14
    相关资源
    最近更新 更多