如何检查 ViewBag 属性是否为空或不存在 [英] How to check if ViewBag property is null or not exists

查看:150
本文介绍了如何检查 ViewBag 属性是否为空或不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 viewbag 属性为 null 或不存在,我需要在 document.ready 函数中执行脚本.下面是我编写的用于检查 viewbag 属性是否不存在的代码.

I have a requirement to execute a script in document.ready function if viewbag property is null or not exists. Below is the code I wrote to check if viewbag property not exists.

我在你@ViewBag.Property!=null的地方使用了推荐方法,但是当我这样做时,我收到一个错误,说当前上下文中不存在名称属性,

I used recommned approached where you @ViewBag.Property!=null but when I do that I get an error saying name property does not exist in current context,

@section scripts {

@if ((bool)ViewData.ContainsKey("FormSubmitFlag") == false)
{
    <script type="text/javascript">
        $(document).ready(function () {

            var pageVisitCount = sessionStorage.getItem("personalDetailsVisitCount");
            if (pageVisitCount == null) {
                $("#personal-details-form").trigger('reset');
                sessionStorage.setItem("personalDetailsVisitCount", "1");
            }
            else {
                var validator = $("#personal-details-form").validate();
                validator.form();
                cat.personaldetails.validate();
            }
        });
    </script>
}

}

谢谢

推荐答案

您可以检查 null 并执行您的脚本.

You can check for null and execute your script.

@if (ViewBag.YourKey== null)
{
 //your code   
}

这将检查 ViewBag.YourKey 是否为空,如果您想检查它是否为空,您可以更改 if 条件.

This will check that ViewBag.YourKey is null if you want to check it for not null you can change the if condition.

这篇关于如何检查 ViewBag 属性是否为空或不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆