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

試著忘記壹切 提交于 2020-01-23 01:42:06

问题


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?


回答1:


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.




回答2:


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



回答3:


TempData Value in Jquery shown below.

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



回答4:


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



回答5:


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



回答6:


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

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

}



来源:https://stackoverflow.com/questions/30057854/how-can-i-check-if-tempdata-is-null-through-integration-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!