首页新闻找找看学习计划

mvc +validate.js验证

0
悬赏园豆:10 [已解决问题] 解决于 2014-03-06 22:38

@model OA.Models.SysLogModel

@{
    ViewBag.Title = "Index";
    Layout = null;
}
<!DOCTYPE html>
<html>
    <head>
        <script src="@Url.Content("~/Script/jquery.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Script/jquery.validate.min.js")" type="text/javascript"></script>

        <script type="text/javascript">
            $(document).ready(function(){
                $("#CreateForm").validate({
                    rules: {
                        Operator: {
                            required: true,
                            minlength: 5
                        },
                        Message: {
                            required: true,
                            maxlength: 4
                        }
                    },
                    messages: {
                        Operator: {
                            required: "请输入标题",
                            minlength: "长度不能小于5"
                        },
                        Message: {
                            required: "请输入年龄",
                            maxlength: "个数小于4"
                        }
                    }
            })
        </script>
    </head>
    <body>
        <h2>Index</h2>

@using (Html.BeginForm("Login", "Account", null, FormMethod.Post, new { Id = "CreateForm" }))
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>SysLogModel</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Operator)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Operator)
            @Html.ValidationMessageFor(model => model.Operator)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Message)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Message)
            @Html.ValidationMessageFor(model => model.Message)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
    </body>
</html>

 代码有点乱,我删除了一些 我点击提交但是不会进行验证,我是哪里错了

还是方法不对

第一次使用

生活还是要继续的主页 生活还是要继续 | 初学一级 | 园豆:10
提问于:2014-01-08 18:51
< >
分享
最佳答案
0

你这么处理有点多余了,因为MVC 已经把验证集成了不需要用户手动去写了用法如下

1 给ViewModel属性加上必填特性

        [DisplayName("PartyA")]
        [Required(ErrorMessage = "PartyA Required")]
        public string PartyA { get; set; }

2 引入Script 文件夹下的两个js文件及jquery.ui的css文件 

<link href="~/Content/jquery.ui/jquery-ui.css" rel="stylesheet" />

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

 

3 view的写法就与你的是一样的了

收获园豆:8
Zery | 大侠五级 |园豆:6069 | 2014-01-08 21:44

谢谢你的回答

还有一个问题是:

验证失败后,还是会提交

生活还是要继续 | 园豆:10 (初学一级) | 2014-01-08 21:49

@生活还是要继续:  那就一定是写法有问题,正常情况下,验证没通过是没法提交的 

Zery | 园豆:6069 (大侠五级) | 2014-01-08 21:50

有QQ吗

弄了好久了,不知道哪里错了

生活还是要继续 | 园豆:10 (初学一级) | 2014-01-08 21:56
其他回答(2)
0

1.建议好好看看validate文档,然后用html简单写一个demo

http://jqueryvalidation.org/validate

秋壶冰月 | 园豆:5443 (大侠五级) | 2014-01-08 20:19
0

同时也注意下插件版本,最新的不支持在IE低版本上验证了。

收获园豆:2
幻天芒 | 园豆:36522 (高人七级) | 2014-01-09 09:47
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册