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

javascript - Check if ViewBag value is null - Stack Overflow

programmeradmin0浏览0评论

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).ready I load the data from ViewBag into View fields. In order to load the date properly, I have to parse the date:

$('#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.MyDate is not null or empty in the following way:

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 if statement and then fails?

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

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

Thanks

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).ready I load the data from ViewBag into View fields. In order to load the date properly, I have to parse the date:

$('#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.MyDate is not null or empty in the following way:

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 if statement and then fails?

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

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

Thanks

Share Improve this question edited Aug 19, 2013 at 12:10 DA_Prog asked Aug 19, 2013 at 6:11 DA_ProgDA_Prog 2833 gold badges7 silver badges14 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

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

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

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.

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:

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

Best solution is to create a boolean variable in razor syntax and set that variable based on that viewbag is null or not and use that variable in script.

@{
string viewBagNull= ViewBag.ViewBagName != null ? true : false;
 }

 <script>
         if(@viewBagNull == false)
         {}
 </script>

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

Script

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

评论列表(0)

  1. 暂无评论