1

当我单击提交客户端验证不起作用并且表单被发布到服务器时,我使用数据注释和部分视图表单创建了模型。从最近几天开始,我对此感到头疼。以下是 _Layout.cshtml 中的 head 部分,其中包含了所有脚本

    <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">

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

4

2 回答 2

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>
于 2013-07-26T13:32:28.743 回答
0

你能发布你的模型代码吗?

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

于 2013-07-24T17:11:49.670 回答