Javascript 如何通过与 jQuery 集成来检查 TempData 是否为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30057854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:35:17  来源:igfitidea点击:

How can I check if TempData is null through integration with jQuery?

javascriptjqueryasp.net-mvcasp.net-mvc-4toastr

提问by The Vanilla Thrilla

I would like to show a toastr(aka a popup) if TempDataisn't null. However, I'm having trouble integrating the jQuery and Razor syntax together. This is my current javascript:

toastr如果TempData不为空,我想显示一个(又名弹出窗口)。但是,我无法将 jQuery 和 Razor 语法集成在一起。这是我目前的 javascript:

$(document).ready(function() {
        if (@TempData["SuccessMessage"] != null) {
            toastr.options = {
                "closeButton": true,
                "positionClass": "toast-bottom-right"
            }
            toastr.success("This is a test!");
        }
});

However, the toastr isn't displaying. I'm already checking TempData further up to also display text to the user.

但是,toastr 没有显示。我已经在进一步检查 TempData 以向用户显示文本。

@if (TempData["SuccessMessage"] != null)
{
    <div class="success-message">
        @Html.Raw(@TempData["SuccessMessage"].ToString())
    </div>
}

I'm wondering if an alternative would be to somehow use the above markup and just check if this div exists, and if so, show the toastr? Or perhaps I can integrate the two checks into one? Suggestions?

我想知道是否有一种替代方法是以某种方式使用上述标记并检查此 div 是否存在,如果存在,则显示 toastr?或者我可以将两张支票合二为一?建议?

回答by The Vanilla Thrilla

I was able to get it working with the following code:

我能够使用以下代码使其工作:

$(document).ready(function() {
    var success = @((TempData["SuccessMessage"] != null).ToString().ToLower());

    if (success == true) {
        toastr.options = {
            "closeButton": true,
            "positionClass": "toast-bottom-right"
        }
        toastr.success("Success!  You're now registered for Lose A Ton!");
    }
});

For anyone curious, I had to call ToLower()because TempData would always return Trueor False, rather than trueor false. The reasons for this are explained here.

对于任何好奇的人,我不得不打电话,ToLower()因为 TempData 总是会返回Trueor False,而不是trueor false此处解释原因。

回答by Tsahi Asher

you should do something like

你应该做类似的事情

if (@(TempData["SuccessMessage"] == null ? "null" : ('"' + TempData["SuccessMessage"] + '"')) != null) {

so that the generated markup in case TempDate is null will be

以便在 TempDate 为 null 的情况下生成的标记将是

if (null != null) {

回答by kavitha Reddy

TempData Value in Jquery shown below.

Jquery 中的 TempData 值如下所示。

$(document).ready(function () {
    var tempdataval = '@TempData["Value"]';
    if (tempdataval != null && tempdataval != '') {
        alert(tempdataval);
    }
});

回答by saif ullah

if (@Html.Raw(Json.Encode(TempData["Exists"])) != null) {

if (@Html.Raw(Json.Encode(TempData["Exists"])) != null) {

   toastr.error('oops!an error occured try again.');

}

}

回答by Jacob Roberts

You can wrap your java in <text></text>to tell Razor that it isn't c#, but inside a c# block

你可以把你的 java 包起来<text></text>告诉 Razor 它不是 c#,而是在 ac# 块内

@if (TempData["SuccessMessage"] != null)
{
<text>
toastr.options = {
            "closeButton": true,
            "positionClass": "toast-bottom-right"
        }
        toastr.success("This is a test!");
</text>
}

You can also convert @(TempData["SuccessMessage"] != null)into a javascript bool and then use a javascript if statement, like so...

您还可以转换@(TempData["SuccessMessage"] != null)为 javascript bool,然后使用 javascript if 语句,就像这样......

var hasSuccessMsg = @(TempData["SuccessMessage"] != null) === 'true';
if (hasSuccessMsg) {
    //do your work here.
}

回答by Harry

`@if (TempData["SuccessMessage"] != null) {
    <script>
       $(document).ready(function() {
            toastr.options = {
                "closeButton": true,
                "positionClass": "toast-bottom-right"
            }
            toastr.success("This is a test!");     
      });
   </script>
 }`