DA_Prog
DA_Prog

Reputation: 283

Check if ViewBag value is null

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

Upvotes: 2

Views: 22037

Answers (5)

Atif Aziz
Atif Aziz

Reputation: 71

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>

Upvotes: 0

knighter
knighter

Reputation: 1227

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.

Upvotes: 2

DA_Prog
DA_Prog

Reputation: 283

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.

Upvotes: 1

Kenneth
Kenneth

Reputation: 28737

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.

Upvotes: 0

Vignesh
Vignesh

Reputation: 1518

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");
  }
});

Upvotes: -1

Related Questions