I'm Trying to check null value within TempData Razor MVC that written inside javascript code, but unfortunately it doesn't work.
whether TempData value is null or not, the condition is always true
if ('@TempData["Error"]' != null) {
alert("Problem" + '\n' + "@TempData["error"]");
} else {
alert("The file Uploaded Successfully");
}
How i can check? if there are alternatives, please let me know.
thanks.
Edit:
The code above is part of JQuery ajax request code.
<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>