【问题标题】:toastr notifications are not working using tempdata attribute使用 tempdata 属性的 toastr 通知不起作用
【发布时间】:2020-05-12 00:10:08
【问题描述】:

我想使用烤面包机显示一些警报消息。从我的控制器,我已将 tempdata 变量传递给查看。我将此变量保留在视图中的 div 属性中。通过使用此属性,我尝试使用烤面包机显示消息。 问题是,tempdata 与 div 属性绑定。但 toasr 脚本不起作用。 下面是视图view.cshtml

    <input type="submit" value="Submit" formaction="Add_Employee" class="btn btn-primary" />
    <div id="msgs" data-message ="@TempData["msg"]"></div>
}

@section Scripts {
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
    <script>
        $(function () {
            $("#datepicker").datepicker();
        });
    </script>
    <script type="text/javascript">
        $(document).ready(function ()

        {
       if($("#msgs").attr("data-message") == "") 
          toastr.error('Server error!! Please try after some time');

        else if ($("#msgs").attr("data-message") == "1")
            toastr.success('Record added successfully');
        else if ($("#msgs").attr("data-message") == "0")
            toastr.warning('Record already exist');
        else
          toastr.error('Operation failed');

    });
    </script>
}

在上面的 id 为“msgs”的数据消息中被绑定了。例如数据消息 =“2”。但不显示通知。提前致谢

【问题讨论】:

  • 控制台有错误吗?你在哪里加载 toastr 库?

标签: html jquery


【解决方案1】:

您应该查看JS if statements syntax。您正在制定条件而无需执行代码...您不能随意将其放置在任何地方。

if (condition) {
  //  block of code to be executed if the condition is true
} 

把你的烤面包机放进去。

就像评论说的,确保你正确地包含了 toastr 库。在没有条件的情况下在页面加载时调用它以查看它是否有效,然后将其包含在 if 条件中,如果您的条件有效。

这里提供的代码可能会出现各种问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    • 2021-10-25
    相关资源
    最近更新 更多