在jquery中使用if语句的问题

时间:2017-02-01 04:45:41

标签: javascript jquery asp.net-mvc

我有一个非常简单的应用程序。我的Home控制器接收Comment对象并运行其逻辑以确定是否需要显示通知。如果答案是肯定的,那么它在ViewBag中设置以下参数:

Team.create(name: 'Los Angeles Dodgers', sport: 'baseball', city: 'Los Angeles', state: 'CA', country: 'United States')
Team.create(name: 'New York Yankees',    sport: 'baseball', city: 'New York',    state: 'NY', country: 'United States')
Team.create(name: 'Chicago Cubs',        sport: 'baseball', city: 'Chicago',     state: 'IL', country: 'United States')
Team.create(name: 'St. Louis Cardinals', sport: 'baseball', city: 'St. Louis',   state: 'MO', country: 'United States')
Player.create(first_name: 'Max',   last_name: 'Walker', full_name_surname_first: 'Walker, Max', ssn: '123-45-6789')
Player.create(first_name: 'Homer', last_name: 'Winn',   full_name_surname_first: 'Winn, Homer', ssn: '234-56-7890')
Player.create(first_name: 'Will',  last_name: 'Steel',  full_name_surname_first: 'Steel, Will', ssn: '345-67-8901')
Player.create(first_name: 'Lucky', last_name: 'Snag',   full_name_surname_first: 'Snag, Lucky', ssn: '456-78-9012')
Role.create(name: 'pitcher')
Role.create(name: 'catcher')
Role.create(name: 'first baseman')
Role.create(name: 'second baseman')
Role.create(name: 'shortstop')
Role.create(name: 'third baseman')
Role.create(name: 'right fielder')
Role.create(name: 'center fielder')
Role.create(name: 'left fielder')
FilledTeamRole.create(team_id: 1, player_id: 1, role_id: 1)
FilledTeamRole.create(team_id: 2, player_id: 2, role_id: 2)
FilledTeamRole.create(team_id: 3, player_id: 3, role_id: 3)
FilledTeamRole.create(team_id: 4, player_id: 4, role_id: 4)

否则,它将参数设置为如下(我随机将所有内容设置为null,以便toDisplayNotification不再为1!)

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

然后显示我所拥有的评论部分视图:

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

所以我面临的问题是,无论toDisplayNotification值如何,视图始终显示通知(我已经测试了我的Home控制器的逻辑并知道它为每个ViewBag属性设置了正确的值),即使toDisplayNotification的值不应为零。

我的ViewBag值是否可能以某种方式被更改(不能来自代码,因为我的Home控制器直接显示部分视图,因此值在转换中应保持不变)或者我在if条件中缺少某些内容?

编辑1 - 回答下面的一些问题。我只使用Newtonsoft.Json.JsonConvert.SerializeObject,因为有人在另一个问题中建议我使用。否则,我不是序列化专家(我发现除非我序列化属性,否则我无法将ViewBag中的非整数值拉入jquery / javascript)。

另外,我确实尝试使用以下任一方法替换toDisplayNotification行,但两个都没有工作:

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

1 个答案:

答案 0 :(得分:1)

试试这个

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

我不完全确定您为何要序列化ViewBag.toDisplayNotification然后将其与数字进行比较。