MVC3 - 实现服务和存储库层后客户端验证不起作用

时间:2012-03-21 04:01:10

标签: asp.net-mvc-3 client-side-validation

我一直在编写我的第一个MVC3应用程序一段时间,我遇到了一个我无法解决的错误。客户端验证似乎不起作用。我一直得到:一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性
我已经对这个问题进行了大量的研究,并且发现了很多有类似问题的人,他们的解决方案对我不起作用。

我的Web配置文件包含:

  <appSettings>
    <add key="webpages:Version" value="1.0.0.0" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="RouteDebugger:Enabled" value="false" />
  </appSettings>

我在所有Model类上都设置了数据注释:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace Mike_Cottingham_Industries.Models
{
    public class Part
    {
        public Int32 PartID { get; set; }

        [Display(Name="Manufacturer Part Number")]
        [Required(ErrorMessage="Manufacturer Part Number is required")]
        [MaxLength(50, ErrorMessage="Manufacturer Part Number cannot exceed 50 characters")]
        public String ManufacturerPartNumber { get; set; }

        [Display(Name="Supplier Part Number")]
        [Required(ErrorMessage="Supplier Part Number is required")]
        [MaxLength(50, ErrorMessage="Supplier Part Number cannot exceed 50 characters")]
        public String SupplierPartNumber { get; set; }

        [Required(ErrorMessage="Description is required")]
        [MaxLength(250, ErrorMessage="Description cannot exceed 250 characters")]
        public String Description { get; set; }

        public virtual ICollection<Product> Products { get; set; }
        public virtual ICollection<PartOrder> PartOrders { get; set; }
    }
}

我在视图中加载了数据验证脚本:

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

我能想到的唯一可能是搞乱它是我尝试制作通用视图的方式。

我创建了一个通用的“创建”视图:

<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((string)@ViewBag.Action, (string)@ViewBag.ControllerName, FormMethod.Post, new { @class="niceform"})) {

    <fieldset class="edit_item">
        @Html.Partial("_Edit");
    </fieldset>
}

<div>   
    <a href="@Url.Action("Index", (string)@ViewBag.ControllerName)" class="bt_green"><span class="bt_green_lft"></span><strong>Back to List</strong><span class="bt_green_r"></span></a>    
    <a href="#" class="bt_blue" onclick="$('.niceform').submit(); return false;"><span class="bt_blue_lft"></span><strong>Save</strong><span class="bt_blue_r"></span></a>    
</div>

由通用视图加载的部分:

@model Mike_Cottingham_Industries.Models.Part     
@{
    ViewBag.Controller = "Part";
    ViewBag.Title = "Part";    
}
@if (ViewBag.IsEdit == true)
{
    @Html.HiddenFor(model => model.PartID)    
}
@Html.ValidationSummary(true)
<dl>
    <dt>@Html.LabelFor(model => model.ManufacturerPartNumber, "Manufacturer Part Number")</dt>
    <dd>@Html.EditorFor(model => model.ManufacturerPartNumber)
        @Html.ValidationMessageFor(model => model.ManufacturerPartNumber)</dd>
</dl>
<dl>
    <dt>@Html.LabelFor(model => model.SupplierPartNumber)</dt>
    <dd>@Html.EditorFor(model => model.SupplierPartNumber)
        @Html.ValidationMessageFor(model => model.SupplierPartNumber)</dd>
</dl>
<dl>
    <dt>@Html.LabelFor(model => model.Description)</dt>
    <dd>@Html.EditorFor(model => model.Description)
        @Html.ValidationMessageFor(model => model.Description)</dd>
</dl>

有没有人对我做错了什么有任何想法?不使用我的'通用'的视图似乎工作正常。

干杯!

1 个答案:

答案 0 :(得分:0)

不确定问题是什么......我将所有代码复制到一个新项目中,一切正常。无法解释有什么不同。