最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I check if TempData is null through integration with jQuery? - Stack Overflow

programmeradmin1浏览0评论

I would like to show a toastr (aka a popup) if TempData isn't null. However, I'm having trouble integrating the jQuery and Razor syntax together. This is my current 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.

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

I would like to show a toastr (aka a popup) if TempData isn't null. However, I'm having trouble integrating the jQuery and Razor syntax together. This is my current 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.

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

Share Improve this question asked May 5, 2015 at 15:57 The Vanilla ThrillaThe Vanilla Thrilla 2,00510 gold badges31 silver badges54 bronze badges 1
  • have you looked at the generated markup? my guess is that you have if ( != null) {, which is obviously invalid. – Tsahi Asher Commented May 5, 2015 at 16:05
Add a comment  | 

5 Answers 5

Reset to default 7

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 True or False, rather than true or false. The reasons for this are explained here.

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

if (null != null) {

TempData Value in Jquery shown below.

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

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

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

var hasSuccessMsg = @(TempData["SuccessMessage"] != null) === 'true';
if (hasSuccessMsg) {
    //do your work here.
}
@if (TempData["SuccessMessage"] != null) {
    <script>
       $(document).ready(function() {
            toastr.options = {
                "closeButton": true,
                "positionClass": "toast-bottom-right"
            }
            toastr.success("This is a test!");     
      });
   </script>
 }
发布评论

评论列表(0)

  1. 暂无评论