Asp.net mvc DataAnnotations-未进行验证
验证在本地box、dev站点上正常工作,但在暂存和生产站点上没有进行。客户端和服务器端验证均未进行。暂存和生产都是负载平衡的,但由于一些其他功能需求,使用粘性连接 我已经检查了所有环境中的bin文件夹,在那里我看到了以下两个DLLAsp.net mvc DataAnnotations-未进行验证,asp.net-mvc,validation,data-annotations,Asp.net Mvc,Validation,Data Annotations,验证在本地box、dev站点上正常工作,但在暂存和生产站点上没有进行。客户端和服务器端验证均未进行。暂存和生产都是负载平衡的,但由于一些其他功能需求,使用粘性连接 我已经检查了所有环境中的bin文件夹,在那里我看到了以下两个DLL DataAnnotationsExtensions.ClientValidation.dll DataAnnotationsExtensions.dll 在服务器端,以下操作应该会失败,但不会发生 !TryValidateModel(model) || !Model
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>
在旧的暂存和生产箱上,验证工作正常,但随后我们在其上安装了MVC3
更新-控制器代码
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差异
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
}
}
看起来验证属性甚至没有进入html,好像站点甚至不知道我们正在使用验证。现在,dev和staging站点都有相同的代码
中转站
<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,我会查看你正在运行的MVC5应用程序的bin。它的.Net程序集是否比您的新应用程序多?如果是这样的话,那么可能有人在其中包含了所有缺少的MVC程序集。您可以尝试从正在工作的mvc应用程序复制所有程序集,只是确保不覆盖。这将向您显示您缺少的任何程序集。由于一些海报没有完整阅读问题和评论并尝试回答,因此我将从问题线索中删除最后两条评论作为答案。我的问题已经解决了 我将web.config从本地计算机移到了staging,prod和validation开始工作。我已经检查了旧的web.config和这个新的工作文件夹web.config,没有区别。尽管它起作用了,我很高兴,但同时我也感到困惑
在本例中,ASP.NET临时文件似乎是一个问题。当我手动更新web.config时,临时文件也得到了更新,这为我解决了这个问题。你能在控制台中显示controllerany错误吗?还有一个愚蠢的问题——你是否将jquery添加到你的页面?我已经用控制器代码更新了这个案例。控制台显示中没有错误。我已经添加了jquery,本地开发人员和开发站点都在工作。您是否在html元素中看到
数据属性
s?@AmirHosseinMehrvarzi在开发人员中是,在登台时否。。。更新2显示了开发人员和登台人员的html。目前,开发和登台站点都有相同的代码基础。我不能在服务器上安装MVC,所有东西都需要放在箱子里。MVC也没有安装在dev-box上,它正在那里工作。我会再看一看垃圾箱文件夹,看看我是否找到了什么。比较两个垃圾箱有什么进展吗?没有。我在我的问题线程中添加了一个更新#2。在查看html时,我没有看到与验证相关的html。dev和staging站点都有相同的代码库。请查看问题线程下与我的web.config相关的注释。
<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>