DataAnnotations - 验证没有发生

DataAnnotations - Validation not happening

验证在本地盒子、开发站点上正常工作,但在暂存站点和生产站点上没有发生。客户端和服务器端验证都没有发生。暂存和生产都是负载平衡的,但由于一些其他功能要求而使用粘性连接。

我检查了所有环境中的 bin 文件夹,我在那里看到了以下两个 dll。

DataAnnotationsExtensions.ClientValidation.dll
DataAnnotationsExtensions.dll

在服务器端,跟随应该会失败,但它并没有发生。

!TryValidateModel(model) || !ModelState.IsValid

此站点使用 windows 身份验证。

Web.config

<appSettings file="Configs\AppSettings_LocalHost.config">
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    </appSettings>

出于测试目的,我目前没有使用捆绑包。对于捆绑包,我什至使用以下

对其进行了测试
<location path="~/Content">
        <system.web>
            <authorization>
                <allow users ="*" />
            </authorization>
        </system.web>
    </location>
    <location path="~/bundles">
        <system.web>
            <authorization>
                <allow users ="*" />
            </authorization>
        </system.web>
    </location>
    <location path="~/Scripts">
        <system.web>
            <authorization>
                <allow users ="*" />
            </authorization>
        </system.web>
    </location>

我还引用了以下 JS 文件

<script src="/NetSite/Scripts/Core/jquery.validate.min.js?v=1.12" type="text/javascript"></script>
<script src="/NetSite/Scripts/Core/jquery.validate.unobtrusive.min.js?v=1.12" type="text/javascript"></script>
<script src="/NetSite/Scripts/Custom/Validators.js?v=1.12" type="text/javascript"></script>

该应用程序是 MVC 5,并且都是通过 NuGet 包添加的。我没有在服务器上安装 MVC。我在这些服务器上有另一个 MVC 5 应用程序,验证进行得很好。

这里是表单标签,第二个工作应用使用相同的表单标签。

using (Html.BeginForm(ActionNames.Index, ControllerNames.Rankings, new { Area = AreaNames.MemberToolsReports }, FormMethod.Post, new { id = "RankingsSearchForm" }))

在旧的暂存和生产箱上,验证工作正常,但后来我们在上面安装了 MVC 3。

更新 - 控制器代码

using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.Mvc;
        using Project.BusinessEntities;
        using Project.Common.Constants;
        using Project.MvcBase;
        using Project.Resources;
        using Project.ServiceInterfaces;
        using Project.ViewModels;
        using Project.ViewModels.MemberToolReports;
        using Microsoft.Practices.Unity;
        using Project.Helpers.Helpers;
        using Project.Helpers.IO;

        namespace Project.Site.Areas.MemberToolsReports.Controllers
        {
            public class RankingsController : BaseController
            {
                #region PROPERTIES

                [Dependency]
                public IGeographyService GeographyServiceInstance { get; set; }

                [Dependency]
                public IRankingsService RankingsServiceInstance { get; set; }

                [Dependency]
                public IUtilityService UtilityServiceInstance { get; set; }

                #endregion

                #region ACTIONS

                public ActionResult Index()
                {
                    var states = getting states here
                    var key = String.Empty;

                    var search = new RankingSearch { Key = key };

                    var model = new RankingSearchViewModel { Search = search, StatesList = states };

                    return View(model);
                }

                [HttpPost]
                [ValidateAntiForgeryToken]
                public ActionResult Index(RankingSearchViewModel model)
                {
                    var errorModel = new ContentShowError { IsError = true };
                    var resultModel = new RankingsSearchResultsViewModel();

                    try
                    {   
                    //TODO: remove extra code once data annotations issue is fixed on staging and prod
                        if (!Request.IsAjaxRequest())
                        {
                            errorModel.Message = base.GetDisplayMessage(ProcessingMessagesEnum.ErrorServicingRequest);
                        }
                        else if (!TryValidateModel(model) || !ModelState.IsValid)
                        {
                            errorModel.Message = base.GetDisplayMessage(ProcessingMessagesEnum.ErrorProcessingRequest);
                        }
                        else if (String.IsNullOrWhiteSpace(model.Search.Key) &&
                                 String.IsNullOrWhiteSpace(model.Search.Institution) &&
                                 String.IsNullOrWhiteSpace(model.Search.State))
                        {
                            errorModel.Message = base.GetDisplayMessage(ProcessingMessagesEnum.NoCriteriaSpecified);
                        }
                        else
                        {
                            //default - debug code
                            errorModel.Message = base.GetDisplayMessage(ProcessingMessagesEnum.ErrorNoDataFound);

                            var results = RankingsServiceInstance.SearchRanking(model.Search);
                            if (results != null && results.Count > 0)
                            {
                                errorModel.IsError = false;
                                errorModel.Message = String.Empty;

                                //update result model
                                resultModel.Rankings = results;

                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        errorModel.Message = base.GetDisplayMessage(ProcessingMessagesEnum.ErrorProcessingRequest);
                        base.LogException(ex);
                    }
                    ActionResult result = null;
                    result = errorModel.IsError ? PartialView(ViewNames.ErrorControl, errorModel) : PartialView(ViewNames.SearchResultsControl, resultModel);

                    return result;
                }

                #endregion

            }
        }

更新 2 - HTML 差异

看起来验证属性甚至没有进入 html,就好像该站点甚至不知道我们正在使用验证一样。现在,开发站点和暂存站点都具有相同的代码。

暂存站点

<input autofocus="autofocus" class="clearSearchFields" id="Search_Key" maxlength="6" name="Search.Key" size="6" type="text" value="" /><br />

工作开发站点

<input autofocus="autofocus" class="clearSearchFields" data-val="true" data-val-length="Key must be 6 characters long" data-val-length-max="6" data-val-length-min="6" data-val-regex="Only alphanumeric (A-Z a-z 0-9) values are allowed" data-val-regex-pattern="[A-Za-z0-9]*" id="Search_Key" maxlength="6" name="Search.Key" size="6" type="text" value="" /><br />
                                <span class="field-validation-valid" data-valmsg-for="Search.Key" data-valmsg-replace="true"></span>

由于您在那台机器上有另一个运行良好的 mvc 5 应用程序,并且您没有安装 MVC,所以看起来有些地方没有正确部署。最有可能的是,MVC 包中需要一些您没有的程序集。

有什么原因不能在服务器上安装 MVC 吗?有独立的软件包可供使用。这应该将所有内容添加到您需要的 GAC。

如果您无法安装 MVC,我会查看您正在运行的 MVC 5 应用程序的垃圾箱。它似乎比您的新应用程序具有更多的 .Net 程序集吗?如果是这样,那么可能有人在其中包含了所有缺失的 MVC 程序集。您可以尝试从工作的 mvc 应用程序复制所有程序集,只要确保您没有覆盖。那应该会显示您缺少的任何程序集。

由于一些发帖人没有阅读完整的问题和评论并试图回答,我将把我最后两条评论从问题线程中移走作为答案。我的问题已解决。

我将 web.config 从我的本地机器移到暂存和生产和验证开始工作。我已经检查了旧的 web.config 和这个新的工作文件夹 web.config,没有区别。尽管它正在工作,但我很高兴,但我现在同时感到困惑。

看起来 ASP.NET 临时文件在这种情况下是个问题。当我手动更新 web.config 时,临时文件也更新了,这解决了我的问题。