简体   繁体   English

如何检查在js代码中编写的TempData Razor MVC中的空值

[英]How to check null value within TempData Razor MVC that written inside js code

I'm Trying to check null value within TempData Razor MVC that written inside javascript code, but unfortunately it doesn't work. 我正在尝试检查用JavaScript代码编写的TempData Razor MVC中的空值,但不幸的是它不起作用。

whether TempData value is null or not, the condition is always true 无论TempData值是否为null,条件始终为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. 上面的代码是JQuery ajax请求代码的一部分。

 <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>

Your quoting the value, so if @TempData["Error"] is null , it translates to a empty string. 您引用的值,因此如果@TempData["Error"]null ,它将转换为空字符串。 You could check it using .length but it would be better to use 您可以使用.length检查,但最好使用

var error = @Html.Raw(Json.Encode(TempData["Error"]))
if (error) {
    alert("Problem" + '\n' + error);
} else {
    alert("The file Uploaded Successfully");
}

Based on the revised context of the question, your using this inside an ajax call. 根据问题的修订上下文,您可以在ajax调用中使用它。 Razor code is parsed on the server before its sent to the view, so @TempData["Error"] returns the value when when the page is first rendered. 剃刀代码在发送到视图之前在服务器上进行了解析,因此@TempData["Error"]在首次呈现页面时返回该值。 Just because you may be setting the value of TempData in the Upload() method does not update it. 仅仅因为您可能在Upload()方法中设置了TempData的值,并不会更新它。

Your method should be returning json containing the error so that you can then display it in the success callback. 您的方法应该返回包含错误的json,以便您可以随后将其显示在成功回调中。 For example, your method might be 例如,您的方法可能是

public ActionResult Upload(...)
{
    return Json(null); // to indicate success, or
    return Json("oops, something went wrong);
}

and then in the ajax callback 然后在ajax回调中

success: function (response) {
    if (response) {
        alert(response);
    } else {
        alert("The file Uploaded Successfully");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM