ASP.NET MVC3验证问题

时间:2011-05-16 10:59:49

标签: c# asp.net validation asp.net-mvc-3

我有以下ViewModel:

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

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

我想在我的视图中使用:

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

    }

但是我的验证没有被触发。无论是客户端还是服务器端。任何人都可以知道为什么?感谢。

2 个答案:

答案 0 :(得分:5)

你需要添加jquery和jQuery Validation插件(以及可选的,不显眼的库)

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

所以,您的观点将如下:

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

}

刚刚添加了您的对象,并创建了一个Add操作,如:

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

使用Create模板和Bulletin1ViewModel类创建了一个视图,如下所示:

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

没有做更多的事情,结果是:

original file

enter image description here

我会重新检查javascript库......

答案 1 :(得分:1)

不要使用

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

但只需从项目的Scripts文件夹中拖放以下.js文件:

jquery-1.4.4.min.js
jquery.validate.min.js
jquery.validate.unobtrusive.min.js

这将确保您对js文件的引用是正确的。