c# - ASP.NET MVC3 验证问题

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

我有以下 ViewModel:

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

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

我想在我的 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>

    }

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

最佳答案

您需要同时添加 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>

因此,您的 View 将是:

@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 类创建了一个 View ,如下所示:

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

关于c# - ASP.NET MVC3 验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6016389/

相关文章:

c# - 如何将查询结果附加到数据表c#

c# - 如何在 C# 中使用开放式硬件监视器源代码?我试过任何方法都不行

c# - 可为空数据类型的 jQuery 验证

c# - PostgreSql 执行标量 ASP.NET

asp.net - 尝试使用 "Insert into"ms-access 数据库时出错

php - 如何验证我的 PHP 代码可能生成的许多可能的 HTML 页面?

c# - IE 11 问题 : Session getting null on Iframe postback. ..?

c# - 将 XML 消息反序列化为对象

Python 属性包 : validators after instantiation

arrays - 使用带有附加参数的自定义规则验证 Laravel 中的数组