ASP.NET MVC3验证问题 [英] ASP.NET MVC3 Validation problem

查看:163
本文介绍了ASP.NET MVC3验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下视图模型:

public class Bulletin1ViewModel
    {
        [Required]
        public String NumberDelegations { get; set; }

        [Required]
        public String TravelPlans { get; set; }
    }

,我想在我看来,使用方法:

Which I want to use in my View:

     @using ErasProject.Models
        @model ErasProject.Models.Bulletin1ViewModel
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

        @using (Html.BeginForm())
        { 
            @Html.ValidationSummary(true)

            <fieldset>

            <p>
            @Html.EditorFor(model => model.NumberDelegations)
            @Html.ValidationMessageFor(model => model.NumberDelegations)
            </p>

            <p>
            @Html.EditorFor(model => model.TravelPlans)
            @Html.ValidationMessageFor(model => model.TravelPlans)
            </p>

            <p>
            <input type="submit" value="Submit" />
            </p>

            </fieldset>

    }

但我的验证不会被触发。无论是客户端还是服务器端。任何人都可以有一个想法,为什么?谢谢你。

But my validation isn't triggered. Neither client-side nor server-side. Anyone could have an idea why? Thanks.

推荐答案

您需要添加两个jQuery和jQuery验证插件(和可选的不显眼库)

you need to add both jquery and jQuery Validation plugin (and optionally, the unobtrusive library)

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"           type="text/javascript"></script>
<script src="http://ajax.microsoft.com/ajax/jQuery.Validate/1.7/jQuery.Validate.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>

这样,你的观点会像:

so, your view will be as:

@using ErasProject.Models
@model ErasProject.Models.Bulletin1ViewModel

<script src="jquery.min.js"></script>
<script src="jQuery.Validate.min.js"></script>
<script src="jquery.validate.unobtrusive.min.js"></script>

@using (Html.BeginForm())
{ 
    @Html.ValidationSummary(true)

    <fieldset>

    <p>
    @Html.EditorFor(model => model.NumberDelegations)
    @Html.ValidationMessageFor(model => model.NumberDelegations)
    </p>

    <p>
    @Html.EditorFor(model => model.TravelPlans)
    @Html.ValidationMessageFor(model => model.TravelPlans)
    </p>

    <p>
    <input type="submit" value="Submit" />
    </p>

    </fieldset>

}


只是添加了对象,并创建了一个添加类似动作:

public ActionResult Add()
{
    return View();
}

和创建使用查看创建模板和 Bulletin1ViewModel 类,它是这样的:

and created a View using the Create template and the Bulletin1ViewModel class that look like this:

@model WebApp_MVC3.Models.Bulletin1ViewModel

@{
    ViewBag.Title = "Add";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Add</h2>

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

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Bulletin1ViewModel</legend>

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

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

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

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

没有做任何事情越多,其结果是:

without doing anything more, the result is:

原始文件

我会重新检查JavaScript库...

I would re-check the javascript libraries...

这篇关于ASP.NET MVC3验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆