[英]Issues with MVC 4 validation attributes on a production server
我已经在这个问题上工作了几天,一直无法找到解决方案。 我将尽力做到尽可能具体,以便您理解问题,并在必要时添加代码。
我有一个带有Html.BeginForm的普通视图(如第一个代码示例所示),其中带有不同的控件,以及其中一些控件的ValidationMessageFors。 在模型(第二个示例)中,我们对某些属性具有几个验证属性,就像其他任何MVC设置一样。 问题在于,在本地运行页面时,data-val-required属性以及其他属性都添加到了html中,但是当部署到生产服务器时,这些属性从不添加,因此验证不会不行 示例3和4展示了这一点。 示例5显示了脚本文件声明。
范例1:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.DocumentID);
@Html.HiddenFor(model => model.TempFilePath);
@Html.HiddenFor(model => model.FileName);
<div style="height: 27px; width: 769px; background-color: #b3b3b3; padding-top: 7px; padding-right: 14px; padding-left: 14px; border-bottom: 1px solid #808080;">
<span style="font-size: 11.5pt; font-weight: bold; float: left;">@ViewBag.Title</span>
</div>
<br />
<div style="margin-left: 15px; margin-right: 15px;">
@Html.ValidationSummary(true)
<h4>Document Name</h4>
@Html.TextBoxFor(model => model.Name, new { id = "DocumentNameEdit" })
@Html.ValidationMessageFor(model => model.Name, "*", new { @style = "font-size:16pt;" })
<br />
<h4>Description</h4>
@Html.TextAreaFor(model => model.Description, new { id = "DocumentDescriptionEdit" })
@Html.ValidationMessageFor(model => model.Description, "*", new { @style = "font-size:16pt;" })
<br />
<input type="button" id="btnSaveDocument" value="Save" />
<input type="button" id="btnClose" value="Close" />
</div>
}
范例2:
public class DocumentModel
{
public DocumentModel()
{
Statuses = new List<SelectListItem>()
{
new SelectListItem() {Text="Visible", Value="3"},
new SelectListItem() {Text="Hidden", Value="4"}
};
}
public string DocumentID { get; set; }
[StringLength(255, ErrorMessage = "Please enter no more than 255 characters for the Name.")]
[Required(ErrorMessage="Name is Required") ]
public string Name { get; set; }
[StringLength(2000, ErrorMessage = "Please enter no more than 2000 characters for the Description.")]
[Required(ErrorMessage = "Description is Required")]
public string Description { get; set; }
public string FileName { get; set; }
public int? Status { get; set; }
public string FolderID { get; set; }
public string TempFilePath { get; set; }
public List<dm_LIST_Folders_Result> FolderList { get; set; }
public List<SelectListItem> Statuses { get; set; }
}
示例3(本地):
<form method="post" action="/DPS/Documents/CreateEdit" novalidate="novalidate">
<input type="hidden" value="TheCode" name="__RequestVerificationToken">
<input type="hidden" value="" name="DocumentID" id="DocumentID">
<input type="hidden" value="" name="TempFilePath" id="TempFilePath">
<input type="hidden" value="" name="FileName" id="FileName">
<div style="height: 27px; width: 769px; background-color: #b3b3b3; padding-top: 7px; padding-right: 14px; padding-left: 14px; border-bottom: 1px solid #808080;">
<span style="font-size: 11.5pt; font-weight: bold; float: left;">Add New Document</span>
</div>
<br>
<div style="margin-left: 15px; margin-right: 15px;">
<h4>Document Name</h4>
<input type="text" value="" name="Name" id="DocumentNameEdit" data-val-required="Name is Required" data-val-length-max="255" data-val-length="Please enter no more than 255 characters for the Name." data-val="true">
<span style="font-size:16pt;" data-valmsg-replace="false" data-valmsg-for="Name" class="field-validation-valid">*</span>
<br>
<h4>Description</h4>
<textarea rows="2" name="Description" id="DocumentDescriptionEdit" data-val-required="Description is Required" data-val-length-max="2000" data-val-length="Please enter no more than 2000 characters for the Description." data-val="true" cols="20"></textarea>
<span style="font-size:16pt;" data-valmsg-replace="false" data-valmsg-for="Description" class="field-validation-valid">*</span>
<br>
<input type="button" value="Save" id="btnSaveDocument">
<input type="button" value="Close" id="btnClose">
</div>
</form>
示例4(生产)
<form method="post" action="/DM/DPS/Documents/CreateEdit" novalidate="novalidate">
<input type="hidden" value="TheCode" name="__RequestVerificationToken">
<input type="hidden" value="" name="DocumentID" id="DocumentID">
<input type="hidden" value="" name="TempFilePath" id="TempFilePath">
<input type="hidden" value="" name="FileName" id="FileName">
<div style="height: 27px; width: 769px; background-color: #b3b3b3; padding-top: 7px; padding-right: 14px; padding-left: 14px; border-bottom: 1px solid #808080;">
<span style="font-size: 11.5pt; font-weight: bold; float: left;">Add New Document</span>
</div>
<br>
<div style="margin-left: 15px; margin-right: 15px;">
<h4>Document Name</h4>
<input type="text" value="" name="Name" id="Name">
<br>
<h4>Description</h4>
<textarea rows="2" name="Description" id="Description" cols="20"></textarea>
<br>
<input type="button" value="Save" id="btnSaveDocument">
<input type="button" value="Close" id="btnClose">
</div>
</form>
范例5:
@Scripts.Render("~/bundles/jquery")
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
为了节省时间,这是我尝试的方法:
最后要注意的是:这是在运行IIS 8的Server 2012计算机上。这不是局部视图。
希望你们中的一个遇到了这个。 我在网上看到的每个地方都说要执行这7个选项之一,但没有一个起作用。
编辑:在研究了很多东西之后(自原始发布以来已经过了几天),我发现window.mvcClientValidationMetadata属性已在javascript中设置为正确的标准,但是缺少了连接,因此在运行验证时,这些字段实际上并未经过验证。
只提供一些线索。
将ClientValidationEnabled设置为true应该会导致创建一个span标签,即使您不包括JQuery并将UnobtrusiveJavaScriptEnabled设置为true
这是创建跨度的方法:
private static MvcHtmlString ValidationMessageHelper(this HtmlHelper htmlHelper, ModelMetadata modelMetadata, string expression, string validationMessage, IDictionary<string, object> htmlAttributes)
{
string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expression);
FormContext clientValidation = htmlHelper.ViewContext.GetFormContextForClientValidation();
if (!htmlHelper.ViewData.ModelState.ContainsKey(fullHtmlFieldName) && clientValidation == null)
return (MvcHtmlString)null;
ModelState modelState = htmlHelper.ViewData.ModelState[fullHtmlFieldName];
ModelErrorCollection modelErrorCollection = modelState == null ? (ModelErrorCollection)null : modelState.Errors;
ModelError error = modelErrorCollection == null || modelErrorCollection.Count == 0 ? (ModelError)null : Enumerable.FirstOrDefault<ModelError>((IEnumerable<ModelError>)modelErrorCollection, (Func<ModelError, bool>)(m => !string.IsNullOrEmpty(m.ErrorMessage))) ?? modelErrorCollection[0];
if (error == null && clientValidation == null)
return (MvcHtmlString)null;
TagBuilder tagBuilder = new TagBuilder("span");
...
return TagBuilderExtensions.ToMvcHtmlString(tagBuilder, TagRenderMode.Normal);
}
也许您正在访问缓存的输出? 检查:
好吧,看来我不小心修复了它。 我对Web配置进行了更改,使其无效,将其推向生产环境,然后出现了与此相关的良好错误。 因此,我解决了这个问题,推高了曾经存在的问题,突然一切正常。
据我所知,IIS正在以某种方式读取不同的Web配置或某种形式的隐藏缓存中的内容,直到有一个无效的IIS才将其清除。 奇怪,但至少现在是固定的。
附带说明一下,我偶然添加了另一个模块节点,该节点导致了web.config错误,试图将runAllManagedModulesForAllRequests设置为true,但这已经存在。 如果其他任何人遇到类似问题,我希望引起web.config错误,然后修复它可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.