数据验证在MVC4网站中无效

时间:2013-07-24 15:06:18

标签: c# jquery asp.net-mvc unobtrusive-validation

我使用数据注释创建模型,在单击提交时使用局部视图形式客户端验证不起作用,表单将发布到服务器。在过去的几天里,我对此表示不满。以下是_Layout.cshtml中的头部分,其中包含了所有脚本

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"
    <link href="~/Images/yellowtribe3.png" rel="shortcut icon" type="image/x-icon" />
    <link href="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Content/css")" rel="stylesheet" type="text/css" />
    <link href="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Content/themes/base/css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/YellowTribes.css")" rel="stylesheet" type="text/css" />        
    <link href="@Url.Content("~/Content/menu3.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/rateit.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/shCore.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/shThemeDefault.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/prettify.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/rateit.css")" rel="stylesheet" type="text/css" />
    <script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Scripts/js")"></script>


    <meta name="viewport" content="width=device-width" />

    @System.Web.Optimization.Scripts.Render("~/bundles/jquery")

    @System.Web.Optimization.Scripts.Render("~/bundles/jquery")

    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.6.4.min.js")"></script>
    <script type="text/javascript">
        $(document).bind("mobileinit", function () {
            $.mobile.ajaxEnabled = true;
            $.extend($.mobile, { ajaxFormsEnabled: true });
        });
    </script>

    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>

以下是我有输入的部分视图代码

<table width="100%" border="0" cellpadding="0" cellspacing="0" align="left">
    <tr>
        <td align="right" width="15%">
            <div>Name:</div> 
        </td>

        <td align="left"> 
            <div class="input200">
                <div class="input200">@Html.EditorFor(x => x.Name, new { @maxlength = "25" })</div> 
            </div> 
        </td>
    </tr>
    <tr>
        <td align="right" valign="top">
            <div>Comment:</div> 
        </td>
        <td align="left"> 
            <div>@Html.TextAreaFor(x => x.CommentText, 5, 50, null)</div> 
        </td>
    </tr>
    <tr>
        <td></td>
        <td>
            @Html.Raw(Html.GenerateCaptcha())
            @Html.ValidationMessage("recaptcha")
        </td>
    </tr>
    <tr>
        <td align="right">
        </td>
        <td align="left"> 
                <input id="ArticleID" name="ArticleID" type="hidden" value="@ViewData["ArticleID"].ToString()" />
            <input class="actionButtons" type="submit" value="Submit" onclick="validateInput();" formmethod="post"/> 
            <div id="thanksMsg" style="display:none; color:Red;">
                <b>Thank you for your comment...</b>
            </div>      
        </td>
    </tr>
    <tr>
        <td></td>
        <td>
            <br /> <br />
        </td>
    </tr>


</table>

如果我查看我的网页来源,看起来似乎没有验证

                <div class="input200"><input class="text-box single-line" data-val="true" data-val-required="Please enter your name" id="Name" name="Name" type="text" value="" /></div> 
            </div> 

            <div><textarea cols="50" data-val="true" data-val-required="Please enter comments" id="CommentText" name="CommentText" rows="5">

当我将这些字段留空时,不确定为什么它不会验证。任何帮助非常感谢。

2 个答案:

答案 0 :(得分:1)

我终于找到了这个问题。问题在于我包含javascripts脚本的方式。 我改变了以下顺序 来自

<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>

<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

答案 1 :(得分:0)

您可以发布型号代码吗?

前几天我做了一些非常愚蠢的事情让我陷入困境一段时间,我没有正确地声明我的属性,我只是用set和get方法创建了公共字符串而不是属性。除了模型绑定和验证之外,使用模型时一切正常。这让我疯了,也许你犯了同样的错误?