在asp.net mvc3中验证问题

时间:2012-05-06 06:28:23

标签: asp.net-mvc-3 c#-4.0 data-annotations

我是system.datamodel.dataannotation,用于验证我的观点。

下面是我的视图模型。

using System.ComponentModel.DataAnnotations;

    public class LoginVM
{
    [Required(ErrorMessage="Please provide UserName")]
    public string UserName { get; set; }
    public string Password { get; set; }
}

下面是我对登录的看法

<% using (Html.BeginForm())
{ %>
<% Html.ValidationSummary(true); %>
<div style="width: 100%; height: 100%; border-style: none;">
    <fieldset style="border-style: none;">
        <table style="width: 100%; height: 100%; border-style: none;" border="0">
            <tr>
                <td style="height: 100px;" colspan="4">
                </td>
                <td class="editor-label" style="width: 35%; font-size: medium;   vertical-align: top;
                    text-align: justify;">
                    <%: Html.Label("Sign in with your Account Credentials")%><br />
                    <br />
                    <a href='<%= Url.Action("LogOn","Site") %>' class="header-links">Support / Contact Us</a>
                </td>
                <td style="width: 5%;">
                </td>
                <td style="width: 60%; border-left-width: 2px; border-left-color: #E3E3E3; border-left-style: solid;">
                    <table style="border-style: none;" border="0">
                        <tr>
                            <td colspan="3">
                            </td>
                        </tr>
                        <tr style="height: 30px;">
                            <td style="width: 20%;" class="login-display-label">
                                <%: Html.LabelFor(m=>m.UserName,"UserName :") %><br />
                                <br />
                                <%: Html.LabelFor(m=>m.Password,"Password :") %><br />
                                <br />
                            </td>
                            <td style="width: 182px;" colspan="2">
                                <%: Html.TextBoxFor(m=>m.UserName,"UserName :")%><br />
                                <%: Html.ValidationMessageFor(m=>m.UserName) %><br />
                                <%: Html.TextBoxFor(m=>m.Password,"Password :")%><br />
                                <%: Html.ValidationMessageFor(m=>m.Password) %><br />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                &nbsp
                            </td>
                            <td style="width: 182px;" colspan="3">
                                <input type="button" id="btnSubmit" value="Submit" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </fieldset>
</div>
    下面是我在我的视图和我的site.master页面上放置的脚本

   <script src="../../Scripts/jquery-1.5.1.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.unobtrusive.js" type="text/javascript"></script>

我还检查了我的web.config以获得以下标记

  <appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>

经过大量的努力,我的验证没有发生,请帮助我....

让我知道我必须做出哪些必要的改变。

提前thnaks

1 个答案:

答案 0 :(得分:1)

更改javascript地址:

<script src="@Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

和Html Helper

Html.TextBoxFor(m=>m.UserName)