javascript 检查 ViewBag 值是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18307383/
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-10-27 11:24:34  来源:igfitidea点击:

Check if ViewBag value is null

javascriptasp.net-mvc-3runtime-errorviewbag

提问by DA_Prog

I have a View in an MVC3 project, in which on load I set an Entity into the ViewBag. One of the entity's properties is of type Datetime?On $(document).readyI load the data from ViewBag into View fields. In order to load the date properly, I have to parse the date:

我在 MVC3 项目中有一个视图,在加载时我将一个实体设置到 ViewBag 中。实体的属性之一是Datetime?On $(document).readyI 将数据从 ViewBag 加载到 View 字段中。为了正确加载日期,我必须解析日期:

$('#date_datepicker').datepicker("setDate", new Date('@ViewBag.Ent.MyDate.Year', '@ViewBag.Ent.MyDate.Month', '@ViewBag.Ent.MyDate.Day'));

Of course, I first check if the value of @ViewBag.Ent.MyDateis not null or empty in the following way:

当然,我首先@ViewBag.Ent.MyDate通过以下方式检查的值是否为空或空:

if ('@ViewBag.Ent.MyDate' != null && '@ViewBag.Ent.MyDate' != '')

Meaning, this is my code:

意思是,这是我的代码:

if ('@ViewBag.Ent.MyDate' != null && '@ViewBag.Ent.MyDate' != '') {
            $('#date_datepicker').datepicker("setDate", new Date('@ViewBag.Ent.MyDate.Year', '@ViewBag.Ent.MyDate.Month', '@ViewBag.Ent.MyDate.Day'));
        }

But out of some reason I get
cannot perform runtime binding on a null reference

但出于某种原因,我
无法对空引用执行运行时绑定

Here's my controller code:

这是我的控制器代码:

public ActionResult PropertiesPage(string id)
    {
         if (!string.IsNullOrEmpty(id))
            {
                int myID = 0;
                int.TryParse(id, out myID);

                ent = myBL.GetByEntID(myID);
                ViewBag.Ent = ent ;
            }

            return View();

    }

Why does the Javascript passes my ifstatement and then fails?

为什么 Javascript 通过我的if语句然后失败?

Edit:I tried, according to Kenneth's answer to change my Javascript to:

编辑:根据肯尼斯的回答,我尝试将我的 Javascript 更改为:

@{if (ViewBag.Ent.MyDate != null) {
            <script type="text/javascript">
                $('#date_datepicker').datepicker("setDate", new Date('@ViewBag.Ent.MyDate.Year', '@ViewBag.Ent.MyDate.Month', '@ViewBag.Ent.MyDate.Day'));
            </script>
        }
}

This one won't cause an error, but it doesn't work (script fails, due to Syntax error). The code generates:

这不会导致错误,但它不起作用(脚本失败,由于语法错误)。代码生成:

<script type="text/javascript">
                $('#date_datepicker').datepicker("setDate", new Date('@ViewBag.Ent.MyDate.Year', '@ViewBag.Ent.MyDate.Month', '@ViewBag.Ent.MyDate.Day'));
            </script>

(meaning, <script>within a <script>)

(意思是,<script>在 a 内<script>

Thanks

谢谢

回答by knighter

What worked for me was to put the script tags around the javascript code, as you showed in your edit:

对我有用的是将脚本标签放在 javascript 代码周围,如您在编辑中所示:

@if (ViewBag.Ent.MyDate != null) {
    <script type="text/javascript">
        $('#InitDate_datepicker').datepicker("setDate", new Date('@ViewBag.Ent.MyDate.Year', '@ViewBag.Ent.MyDate.Month', '@ViewBag.Ent.MyDate.Day'));
    </script>
}

but I pulled the entire if block outside of my existing script block, so it isn't double-nested.

但是我将整个 if 块拉到了现有脚本块之外,因此它不是双重嵌套的。

回答by Kenneth

You're mixing up JavaScript code and view code. Your razor-code will not be executed at the same time as your JavaScript code. what you need to do is first evaluate the razor-code on the server, and let that code emit JavaScript:

您正在混淆 JavaScript 代码和视图代码。您的 razor 代码不会与您的 JavaScript 代码同时执行。您需要做的是首先评估服务器上的 razor 代码,然后让该代码发出 JavaScript:

@if (ViewBag.Ent.MyDate != null) {
            $('#InitDate_datepicker').datepicker("setDate", new Date('@ViewBag.Ent.MyDate.Year', '@ViewBag.Ent.MyDate.Month', '@ViewBag.Ent.MyDate.Day'));
}

The if-statement will get executed on the server. If MyDate is null, the second statement doesn't get executed, if it isn't it will execute the statement and send the resulting JavaScript down to the brwoser, with the values filled in.

-if语句将在服务器上执行。如果 MyDate 为 null,则不会执行第二条语句,否则将执行该语句并将结果 JavaScript 发送到 brwoser,并填充值。

回答by DA_Prog

I ended up finding a creative solution. Instead of generating script by the "if"'s result, I added hidden fields according to if statement and than checked the hidden field value.

我最终找到了一个创造性的解决方案。我没有根据“if”的结果生成脚本,而是根据 if 语句添加隐藏字段,然后检查隐藏字段值。

回答by Vignesh

Actually In controller you have set the ViewBag.Ent as the dynamic expression but in the view page you are adding "ViewBag.Ent.MyDate" for accessing it, So this wont work.Use your code like this

实际上,在控制器中,您已将 ViewBag.Ent 设置为动态表达式,但在视图页面中,您正在添加“ViewBag.Ent.MyDate”以访问它,因此这将不起作用。使用您的代码如下

Script

脚本

$(document).ready(function () {
  if ("@ViewBag.Ent" != null) {
     alert("@ViewBag.Ent");
  }
});