0

I have a very simple application. My Home controller receives a Comment object and runs its logic to determine whether a notification needs to be displayed. If the answer is yes, then it sets the following parameters in ViewBag:

ViewBag.toDisplayNotification = 1;
ViewBag.notificationTitle = "This is the title";
ViewBag.notificationId = 2;

else, it sets the parameters as follow (I randomly set everything to null so that toDisplayNotification wouldnt be 1 anymore!)

ViewBag.toDisplayNotification = null;
ViewBag.notificationTitle = null;
ViewBag.notificationId = null;

It then displays the Comment partial View in which I have:

<script>
    $(function myfunction() {

        var toDisplayNotification = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(ViewBag.toDisplayNotification));
        var notificationTitle = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(ViewBag.notificationTitle));
        var notificationId = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(ViewBag.notificationId));

        if(toDisplayNotification == 1){
            var n = new Notification(notificationTitle, {
                body: "This is where the body goes",
                icon: '@Url.Action("GetImageByNotificationId", "Image", new { id = ViewBag.notificationId})'
            });
        }
    });
</script>

So the issue I'm facing is that the view, regardless of the toDisplayNotification value, always displays the notification (I have tested the logic of my Home controller and know that it sets the correct values to each ViewBag property) even when the value of toDisplayNotification shouldnt be zero.

Is it possible that my ViewBag values are being changed somehow (can't be from the code since my Home controller displays the partial view directly so the values should remain unchanged in the transition) or am I missing something in my if condition?


Edit 1 - To answer some of the questions below. I am only using Newtonsoft.Json.JsonConvert.SerializeObject because someone in a different question had suggested that I use. Otherwise, I'm not a serialization expert (What I find is that unless, I serialize the property, I cannot pull non-integer values out of ViewBag into jquery/javascript).

Also, I did try replacing the toDisplayNotification line with either of the following but neither one worked:

var toDisplayNotification = @ViewBag.toDisplayNotification;
//or 
var toDisplayNotification = @Html.Raw(ViewBag.toDisplayNotification);
4
  • 1
    Where are you calling myfunction()? (and note that you can just use var notificationTitle = '@ViewBag.notificationTitle') Commented Feb 1, 2017 at 4:49
  • if you console.log(toDisplayNotification) what does it look like? Also why are you using Newtonsoft to serialize the object? Commented Feb 1, 2017 at 4:51
  • Re your edit - if they are strings, you just need to quote it (see comment above). But what is console.log(toDisplayNotification); etc returning? Commented Feb 1, 2017 at 5:09
  • So turns out that my partial view somehow gets called multiple times with different values for toDisplayNotification. So the if condition is working properly but the logic isnt. Now I need to figure out why/how the view gets called more than once. Thx for everyone's help on this. Commented Feb 1, 2017 at 5:54

1 Answer 1

1

Try this

   var toDisplayNotification = @Html.Raw(ViewBag.toDisplayNotification);
   if(toDisplayNotification == 1){
        var n = new Notification(notificationTitle, {
            body: "This is where the body goes",
            icon: '@Url.Action("GetImageByNotificationId", "Image", new { id = ViewBag.notificationId})'
        });
    }

I'm not entirely sure why you are serializing ViewBag.toDisplayNotification and then comparing it to a number.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.