如何将数据从MVC控制器传递到Telerik UI以获取MVC通知窗口小部件

时间:2015-05-19 19:54:59

标签: kendo-ui telerik telerik-mvc

我在ASP.Net MVC应用程序中使用 TelerikUI for MVC 库。在我的某个页面中,我需要使用通知小部件。有没有办法可以将数据(显示)从MVC控制器传递到我的Razor视图中实例化的Notification小部件?

感谢您的帮助!

山姆

1 个答案:

答案 0 :(得分:1)

我的解决方法。它有效。

首先创建一个BaseController,从中扩展所需的控制器,然后添加以下类属性和方法:

public class MyBaseController : Controller
{

    // set js kendo notifications thru controllers
    public class notification {
        public string Title { get; set; }
        public string Message { get; set; }
        public string Type { get; set; }
    }

    public IList<notification> kendoNotifications;

    public void NewNotification(string title, string message, string type)
    {
        if (kendoNotifications == null)
        {
            this.kendoNotifications = new List<notification>();
        }
        this.kendoNotifications.Add(new notification { Title = title, Message = message, Type = type });
    }
    public IList<notification> GetKendoNotifications()
    {
        return this.kendoNotifications;
    }

    // GET: MyBase
    public MyBaseController()
    {
        // my stuff
        // ViewBag.KendoNotifications = this.kendoNotifications;
    }

接下来在您的布局中包含kendo通知和以下代码:

<script>
        var notification;// global, do not name anything else like this
    </script>
    @Html.Partial("_KendoNotification")
    @if (ViewBag.KendoNotifications != null)
    {
        if (ViewBag.KendoNotifications.Count > 0)
        {
            <script>
                $(document).ready(function () {
                    @*Ver MyBaseController *@
                    @foreach (var notification in ViewBag.KendoNotifications)
                    {
                        <text>
                            notification.show({
                                title: "@notification.Title",
                                message: "@notification.Message",
                            }, "@notification.Type");
                        </text>

                    }
                });// fin ready
            </script>
            ViewBag.KendoNotifications.Clear();
        }
    }

部分_KendoNotification的代码(prety much js widget config)

<div id="kendo-notification-wrap">

<span id="notification" style="display:none;"></span>

<script id="emailTemplate" type="text/x-kendo-template">
    <div class="new-mail">
        <span class="k-icon k-i-email k-icon-32"></span>
        <h3>#= title #</h3>
        <p>#= message #</p>
    </div>
</script>

<script id="errorTemplate" type="text/x-kendo-template">
    <div class="wrong-pass">
        <span class="k-icon k-i-error k-icon-32"></span>
        <h3>#= title #</h3>
        <p>#= message #</p>
    </div>
</script>

<script id="successTemplate" type="text/x-kendo-template">
    <div class="success">
        <span class="k-icon k-i-check-outline k-icon-32"></span>
        <h3>#= message #</h3>
    </div>
</script>

<script>
      $(document).ready(function() {

                notification = $("#notification").kendoNotification({
                    position: {
                        pinned: true,
                        top: 30,
                        right: 15
                    },
                    autoHideAfter: 5000,
                    allowHideAfter: 1000,
                    stacking: "down",
                    templates: [{
                        type: "info",
                        template: $("#emailTemplate").html()
                    }, {
                        type: "error",
                        template: $("#errorTemplate").html()
                    }, {
                        type: "success",
                        template: $("#successTemplate").html()
                    }]

                }).data("kendoNotification");

                $("#hideAllNotifications").click(function(){
                    notification.hide();
                });

                $(document).one("kendo:pageUnload", function(){ if (notification) { notification.hide(); } });



            });
</script>

<style>

    /* Info template */
            .k-notification-info.k-group {
                background: rgba(0%,0%,0%,.7);
                color: #fff;
            }
            .new-mail {
                width: auto;
                height: auto;
            }
            .new-mail h3 {
                font-size: 1em;
                padding: 32px 10px 5px;
            }
            .new-mail img {
                float: left;
                margin: 30px 15px 30px 30px;
            }

            /* Error template */
            .k-notification-error.k-group {
                background: rgba(100%,0%,0%,.7);
                color: #ffffff;
            }
            .wrong-pass {
                width: auto;
                height: auto;
            }
            .wrong-pass h3 {
                font-size: 1em;
                padding: 32px 10px 5px;
            }
            .wrong-pass img {
                float: left;
                margin: 30px 15px 30px 30px;
            }

            /* Success template */
            .k-notification-success.k-group {
                background: rgba(0%,60%,0%,.7);
                color: #fff;
            }
            .success {
                width: auto;
                height: auto;
                padding: 0 30px;
                line-height: 75px;
            }
            .success h3 {
                font-size: 1.7em;
                font-weight: normal;
                margin-bottom: 20px;
                display: inline-block;
                vertical-align: middle;
            }
            .success img {
                display: inline-block;
                vertical-align: middle;
                margin-right: 10px;
            }

</style>

完成此操作后,只需在要将通知传递给视图的任何控制器/操作中执行以下操作:

public class MyTestController : MyBaseController
{
    // GET: Test
    public ActionResult Index()
    {


        this.NewNotification("hola", "prueba", "success");
        ViewBag.KendoNotifications = this.GetKendoNotifications();

        return View("~/Views/Test/Index.cshtml");
    }
}

Notificaction image test

问候!