我已经在这个问题上工作了好几天了,一直无法找到解决方案。我将尝试尽可能具体,以便您了解问题,并在必要时包含代码。
我有一个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中的正确rullings,但是缺少连接,所以在运行验证时,字段实际上没有经过验证。
答案 0 :(得分:0)
只提供一些线索。
将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);
}
也许您正在访问缓存的输出?检查:
答案 1 :(得分:0)
从我可以看出的是,某种程度上IIS正在读取不同的Web配置或某种隐藏缓存中的某些东西,直到有一个无效的它被清除掉它。奇怪,但至少现在已经修复了。
作为旁注,我在事故中添加了第二个模块节点,导致web.config错误,试图将runAllManagedModulesForAllRequests设置为true,但那已经存在。如果其他人遇到类似问题,我希望导致web.config错误然后修复它会修复您的问题。