那么如何在没有连接的视图模型的情况下最简单地在视图上显示用户错误?
在我看来我正在使用这样的上传:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
@(Html.Kendo().Upload()
.Name("files")
.Async(a => a
.Save("CompanyLogoSave", "Company")
.AutoUpload(true)
)
)
}
将值发布到我的控制器,如果它不是图像,我就这样做
else
{
ModelState.AddModelError("Error", "You can only upload images!");
ViewData.Add("UploadError", "You can only upload images!");
return View("Logo", files);
}
我尝试了10种不同的方法来显示我的观点错误:
@if (ViewBag.UploadError != null)
{
<div>
@ViewBag.UploadError
</div>
}
或
@{
if(!ViewData.ModelState.IsValid)
<script>
alert(@ViewBad.UploadError)
</script>
}
我还试图在上传收到错误时调用此js
<script>
function showAlertWindow(e) {
console.log(e);
var alertWindow = $('#alertWindow').data('kendoWindow');
alertWindow.content(error_handler(e));
alertWindow.refresh();
alertWindow.center();
alertWindow.open();
}
function getFileInfo(e) {
return $.map(e.files, function (file) {
var info = file.name;
// File size is not available in all browsers
if (file.size > 0) {
info += " (" + Math.ceil(file.size / 1024) + " KB)";
}
return info;
}).join(", ");
}
function error_handler(e) {
if (e.errors) {
alert("2");
var message = "Errors:\n <ul>";
$.each(e.errors, function (key, value) {
console.log("3");
if ('errors' in value) {
$.each(value.errors, function () {
message += "<li>" + this + "</li>";
});
}
});
showAlertWindow(message + "</ul>");
}
}
</script>
但没有任何运气。