如何检查在 js 代码中编写的 TempData Razor MVC 中的空值
How to check null value within TempData Razor MVC that written inside js code
我正在尝试检查 javascript 代码中编写的 TempData Razor MVC 中的空值,但不幸的是它不起作用。
无论TempData值是否为空,条件总是为真
if ('@TempData["Error"]' != null) {
alert("Problem" + '\n' + "@TempData["error"]");
} else {
alert("The file Uploaded Successfully");
}
我如何检查?
如果有其他选择,请告诉我。
谢谢。
编辑:
以上代码是JQueryajax请求代码的一部分。
<script>
$body = $("body");
$(document).on({
ajaxStart: function () { $body.addClass("loading"); },
ajaxStop: function () { $body.removeClass("loading"); }
});
$(document).ready(function () {
$("#upload").click(function () {
var data = new FormData();
//Add the Multiple selected files into the data object
var files = $("#files").get(0).files;
for (i = 0; i < files.length; i++) {
data.append("files" + i, files[i]);
}
//data.append("id", '');
data.append("id", '@Model.NoteID');
//Post the data (files) to the server
if (files.length > 0) {
$.ajax({
type: 'POST',
url: "@Url.Action("Upload","Files")",
data:data,
contentType: false,
processData: false,
success: function (data) {
if ('@TempData["Error"]' != null) {
alert("Problem" + '\n' + "@TempData["error"]");
} else {
alert("file uploaded successfully");
}
},
error: function () {
alert("Fail");
},
});
}
});
});
</script>
你引用了值,所以如果 @TempData["Error"]
是 null
,它会转换为空字符串。您可以使用 .length
检查它,但最好使用
var error = @Html.Raw(Json.Encode(TempData["Error"]))
if (error) {
alert("Problem" + '\n' + error);
} else {
alert("The file Uploaded Successfully");
}
根据修改后的问题上下文,您在 ajax 调用中使用了它。 Razor 代码在发送到视图之前在服务器上进行解析,因此 @TempData["Error"]
returns 页面首次呈现时的值。仅仅因为您可能在 Upload()
方法中设置 TempData
的值,不会更新它。
您的方法应该返回包含错误的 json,这样您就可以在成功回调中显示它。例如,您的方法可能是
public ActionResult Upload(...)
{
return Json(null); // to indicate success, or
return Json("oops, something went wrong);
}
然后在 ajax 回调中
success: function (response) {
if (response) {
alert(response);
} else {
alert("The file Uploaded Successfully");
}
我正在尝试检查 javascript 代码中编写的 TempData Razor MVC 中的空值,但不幸的是它不起作用。
无论TempData值是否为空,条件总是为真
if ('@TempData["Error"]' != null) {
alert("Problem" + '\n' + "@TempData["error"]");
} else {
alert("The file Uploaded Successfully");
}
我如何检查? 如果有其他选择,请告诉我。
谢谢。
编辑:
以上代码是JQueryajax请求代码的一部分。
<script>
$body = $("body");
$(document).on({
ajaxStart: function () { $body.addClass("loading"); },
ajaxStop: function () { $body.removeClass("loading"); }
});
$(document).ready(function () {
$("#upload").click(function () {
var data = new FormData();
//Add the Multiple selected files into the data object
var files = $("#files").get(0).files;
for (i = 0; i < files.length; i++) {
data.append("files" + i, files[i]);
}
//data.append("id", '');
data.append("id", '@Model.NoteID');
//Post the data (files) to the server
if (files.length > 0) {
$.ajax({
type: 'POST',
url: "@Url.Action("Upload","Files")",
data:data,
contentType: false,
processData: false,
success: function (data) {
if ('@TempData["Error"]' != null) {
alert("Problem" + '\n' + "@TempData["error"]");
} else {
alert("file uploaded successfully");
}
},
error: function () {
alert("Fail");
},
});
}
});
});
</script>
你引用了值,所以如果 @TempData["Error"]
是 null
,它会转换为空字符串。您可以使用 .length
检查它,但最好使用
var error = @Html.Raw(Json.Encode(TempData["Error"]))
if (error) {
alert("Problem" + '\n' + error);
} else {
alert("The file Uploaded Successfully");
}
根据修改后的问题上下文,您在 ajax 调用中使用了它。 Razor 代码在发送到视图之前在服务器上进行解析,因此 @TempData["Error"]
returns 页面首次呈现时的值。仅仅因为您可能在 Upload()
方法中设置 TempData
的值,不会更新它。
您的方法应该返回包含错误的 json,这样您就可以在成功回调中显示它。例如,您的方法可能是
public ActionResult Upload(...)
{
return Json(null); // to indicate success, or
return Json("oops, something went wrong);
}
然后在 ajax 回调中
success: function (response) {
if (response) {
alert(response);
} else {
alert("The file Uploaded Successfully");
}