kendo upload必填验证
@using Kendo.Mvc.UI @using StudentManage.Common.Helper @model StudentManage.Models.Home.ImportDataFromFileModel <style> .hiddenBox { display: block; border: none; width: 0; height: 0 } </style> <script> var fileName; function onSelect(e) { fileName = $("#FileName").val() + getFileInfo(e); $("#FileName").val(fileName); } function onRemove(e) { fileName = $("#FileName").val().replace(getFileInfo(e), ""); $("#FileName").val(fileName); } function getFileInfo(e) { return $.map(e.files, function (file) { var info = file.name; return info; }).join(", "); } </script> <form action="@Url.Action("ImportDataFromFile", "Home")" id="importForm" method="post" class="panel panel-default form-horizontal panel-body"> <div class="form-group"> @Html.RequiredIndicatorLabelFor(m=> m.FileName, new { @class = "col-sm-3 control-label no-padding-right" }) <div class="col-sm-4"> @Html.HiddenFor(m => m.DataSystemName, new { @class = "form-control" }) @(Html.Kendo().Upload().Name("files").Multiple(false).HtmlAttributes(new { accept = ".xls,.xlsx" }) .Events(events => events .Remove("onRemove") .Select("onSelect") )) @Html.TextBoxFor(m => m.FileName, new { @class = "hiddenBox" }) @Html.ValidationMessageFor(m => m.FileName) </div> </div> <div class="form-group"> <div class="col-sm-5 col-sm-offset-3"> <button class="btn btn-info" type="button" id="submitBtn"> 确定 </button> </div> </div> </form> <script> $(function () { jQuery.validator.unobtrusive.parse(); $('#importForm').removeData('validator'); $('#importForm').removeData('unobtrusiveValidation'); $.validator.unobtrusive.parse('#importForm'); $("#submitBtn").click(function() { if (!$("#importForm").valid()){ return false; } $("#importForm").submit(); return true; }); }); </script>
Action
#region 导入导出数据 [HttpGet] public ActionResult ImportDataFromFile(string dataSystemName) { ImportDataFromFileModel model = new ImportDataFromFileModel() { DataSystemName = dataSystemName }; return PartialView(model); } [HttpPost] public async Task<ActionResult> ImportDataFromFile(IEnumerable<HttpPostedFileBase> files) { //DataTable dataTable = await NpoiHelper.ExcelToDataTable("Sheet1", true, files.First()); //for (int i = 0; i < dataTable.Rows.Count; i++) //{ // //var enterprise = new Enterprise(); // //enterprise.Id = Guid.NewGuid(); // //if (dataTable.Rows[i][enterpriseNameColumn] != null) // //{ // // var content = dataTable.Rows[i][enterpriseNameColumn]?.ToString().Trim(); // // var clearName = content.Length < 100 ? content : content.Substring(0, 100); // // if (enterpriseNames.Contains(clearName)) continue; // // enterprise.EnterpriseName = clearName; // //} //} return RedirectToAction("Index","StudentManage"); } #endregion