如何解决未显示必填字段验证错误消息
我正在尝试向客户端显示必填字段的错误消息,但它不起作用,我不确定我是否错过了一个步骤,或者它是否与表单数据是多部分。模型类具有使用 DataAnnotations 所需的标记:
public class SupportRequestModel
{
public int? Id { get; set; }
[required(ErrorMessage = "Request Type is required.")]
public int RequestTypeId { get; set; }
}
该表单同时具有 ValidationSummary() 和 ValidationMessageFor():
@using (Html.BeginForm("SaveSupportRequest","Request",FormMethod.Post,new { id = "addForm",enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true,"",new { @class = "text-danger" })
@Html.HiddenFor(x => x.Id,new { Id = "Id" })
<div class="kt-portlet__body">
<div class="row form-group">
<div class="col-sm-3">
<label class="kt-font-bold">Type:<span class="required">*</span></label>
<select class="form-control" id="requestType" name="requestType"></select>
@Html.HiddenFor(x => x.RequestTypeId,new { RequestTypeId = "RequestTypeId" })
@Html.ValidationMessageFor(m => m.RequestTypeId,new { @class = "text-danger" })
我的 web.config 包含以下键:
<add key="ClientValidationEnabled" value="true" />
需要注意的是,表单是使用 DropzoneJs 提交的以上传文件。这是 Dropzone 的 jQuery:
var myDropzone = new Dropzone('#addForm',{
paramName: "Files",clickable: id + " .dropzone-select",previewTemplate: previewTemplate,previewsContainer: id + " .dropzone-items",autoprocessQueue: false,uploadMultiple: true,parallelUploads: 100,maxFiles: 100,url: "@Url.Action("SaveSupportRequest","Request")",maxFilesize: 100,//max file size in MB,dictResponseError: 'Server not Configured',acceptedFiles: ".png,.jpg,.gif,.bmp,.jpeg,.pdf,.doc,.docx,.xls,.xlsx,gif,mpg,mpeg,.tif,.tiff,.ppt,.pptx",init: function () {
var self = this;
var submitButton = document.getElementById("saveRequest");
self.options.addRemoveLinks = false;
self.on("addedfile",function (file) {
});
self.on("sending",function (file) {
console.log('upload started',file);
$('.meter').show();
});
self.on("totaluploadprogress",function (progress) {
console.log("progress ",progress);
$('.roller').width(progress + '%');
});
self.on("queuecomplete",function (progress) {
$('.meter').delay(999).slideUp(999);
});
self.on("removedfile",function (file) {
console.log(file);
});
var submitButton = document.getElementById("saveRequest");
submitButton.addEventListener("click",function (e) {
e.preventDefault();
e.stopPropagation();
self.processQueue();
self.options.autoprocessQueue = true;
});
this.on("sendingmultiple",function (data,xhr,formData) {
});
self.on("successmultiple",function (files,response) {
});
},success: function (file,response) {
swal.fire({
"title": "","text": "The request has been saved!","type": "success","confirmButtonClass": "btn btn-secondary","confirmButtonText": 'Close',});
$("#ViewSupportModal").modal('toggle');
}
})
并且jQuery文件包含在_Layout.cshtml页面的顶部:
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/webfont/1.6.16/webfont.js"></script>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。