如何使用Kendo Mvc绑定按钮单击数据

时间:2016-12-07 12:28:20

标签: c# asp.net-mvc kendo-grid

我想在单击搜索按钮时将数据显示到kendo网格中,但它只返回浏览器上的json结果。我在控制台上没有任何错误。我想要的就是能够在Kendo网格上返回搜索结果。我试图在页面加载时加载数据并且它工作正常,但它不适用于按钮单击。

如何在单击按钮时在网格上显示数据?

我的观点

 <div>
    </fieldset>
   @using (Html.BeginForm("SearchNotification", "Notification",  FormMethod.Post, new { role = "form" }))
                {

                    <div class="box-footer">
                        <button type="submit" id="btnSearch" class="btn btn-primary">Search</button>
                    </div>

                    <form role="form">
                        <div class="box-body">
                            <div id="divMain" class="col-md-13">
                                <div class="form-group">
                                    @(Html.Kendo().Grid<TTAF.Portal.Parts.Web.Models.NotificationModel.notification>()
                                  .Name("Grid")
                                  .Columns(columns =>
                                  {
                                      columns.Bound(x => x.distributorID).Title("Distributor Code").Width(50);
                                      columns.Bound(x => x.notificationDate).Title("Date").Width(50);
                                      columns.Bound(x => x.notificationType).Title("Type").Width(50);
                                      columns.Bound(x => x.distributorName).Title("Name").Width(50);
                                  })
                                  .AutoBind(false)
                                  .Pageable(pageable => pageable
                                    .Refresh(true)
                                    .PageSizes(true)
                                    .ButtonCount(5))
                                  .Scrollable()
                                  .Filterable()
                                  .Sortable()
                                  .Resizable(resize => resize.Columns(true))
                                  .DataSource(dataSource => dataSource
                                  .Ajax()
                                  .PageSize(10)
                                  .ServerOperation(false)//No post back
                                  .Read(read => read.Action("SearchNotification", "Notification")))) 

                                </div>
                                <br />
                            </div>
                        </div>
                    </form>
                }
            </div>
        </div>
    </fieldset>
</div>

<!--Grid Stylesheet Start-->
<link href="~/Content/Kendo/Kendo-fieldsetStyle.css" rel="stylesheet" />
<link href="~/Content/Kendo/kendo.common.min.css" rel="stylesheet" />
<link href="~/Content/Kendo/kendo.default.min.css" rel="stylesheet" />
<link href="~/Content/Kendo/kendo.rtl.min.css" rel="stylesheet" />
<!--Grid Stylesheet End-->


@section Styles
{
    @Styles.Render("~/bundles/kendo")
    @Scripts.Render("~/bundles/jqueryval")
}

<!--Script Section Start-->
@section Scripts {
    <script src="~/Content/Kendo/js/kendo.all.min.js"></script>
    <script src="~/Content/Kendo/js/kendo.aspnetmvc.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function () {
        });
    </script>

<!--Script Section End-->

    }

控制器

  [HttpPost]
    public ActionResult SearchNotification([DataSourceRequest]DataSourceRequest request, NotificationModel model)
    {

        try
        {
            string jsonReq = ApiBaseUrl + ApiSecuritySubBaseUrl + "/GetDistributorsForAdminUser/" + User.Identity.GetUserId() + "/" + User.Identity.GetUserId();
               string jsonResp = JsonWrapper.JsonGET(jsonReq);

            List<DistributorSimple> ListDistrubutor = Models.DeserialiseFromJson<List<DistributorSimple>>.Deserialise(jsonResp);
            model._ListDistributor = ListDistrubutor;

            List<NotificationProcess> ListNotifiaction = Models.DeserialiseFromJson<List<NotificationProcess>>.Deserialise(jsonResp);
            model._ListNotificationProcess = ListNotifiaction;

            List<string> ListnotificationType = Models.DeserialiseFromJson<List<string>>.Deserialise(jsonResp);
            model._ListNotificationType = ListnotificationType;

            jsonReq = ApiBaseUrl + ApiGeneralSubBaseUrl + "/GetAdminNotificationsByDate";

            JsonParamBuilder myBuilder = new JsonParamBuilder();
            myBuilder.AddParam<string>("submittingUserID", User.Identity.GetUserId().ToString());
            myBuilder.AddParam<string>("userName", User.Identity.GetUserName().ToString());
            myBuilder.AddParam<int>("distributorID", int.Parse(model.SelectedDistributor));
            myBuilder.AddParam<string>("notificationsFromDate", model.DateFrom.ToString("dd/MM/yyyy"));
            myBuilder.AddParam<string>("notificationsToDate", model.DateTo.ToString("dd/MM/yyyy"));
            myBuilder.AddParam<int>("processID", int.Parse(model.SelectedNotificationPrcoess));
            myBuilder.AddParam<string>("notificationType", model.SelectedNotificationType);
            Console.WriteLine("Builder result : " + myBuilder.GetJSonParam());

             string resp = JsonWrapper.JsonPOST(ApiBaseUrl + ApiGeneralSubBaseUrl + "/GetAdminNotificationsByDate", myBuilder.GetJSonParam());
            List<NotificationDetail> _ListNotifications = Models.DeserialiseFromJson<List<NotificationDetail>>.DeserialiseApiResponse(resp);

            List<Models.NotificationModel.notification> listData1 = new List<Models.NotificationModel.notification>();
            foreach (NotificationDetail item in _ListNotifications)
            {
                Models.NotificationModel.notification sngData1 = new Models.NotificationModel.notification();
                sngData1.notificationID = item.notificationID;
                sngData1.notificationMessage = item.notificationMessage;
                sngData1.notificationType = item.notificationType;
                sngData1.processName = item.processName;
                sngData1.notificationDate = item.notificationDate;

                listData1.Add(sngData1);
            }
            NotificationModel.GetAllNotications = listData1;
            return Json(listData1.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

模型

public class NotificationModel
{

    public static List<notification> GetAllNotications { get; set; }
    public class notification
    {
        public int distributorID { get; set; }
        public string distributorName { get; set; }
        public int notificationID { get; set; }
        public string notificationMessage { get; set; }
        public string notificationType { get; set; }
        public int processID { get; set; }
        public string processName { get; set; }
        public DateTime notificationDate { get; set; }

    }
}

1 个答案:

答案 0 :(得分:1)

非常简单。 首先,你不需要

  • Html.BeginForm声明删除此 - 无需使用此kendo在数据源中使用其读取方法来处理帖子
  • 你也不需要这个形式的角色=&#34;形式&#34;删除这个

现在您只需要将onclick事件附加到按钮,然后使用Javascript启动对kendo网格的读取和刷新。请参阅下面的代码段

步骤:1 将onclick事件附加到您的cshtml文件中的按钮

<button id="btnSearch"  onclick="myFunction()" class="btn btn-primary">Search</button>    

步骤:2 编写一个Javascript方法myFunction()来处理click事件并刷新网格

<script>
function myFunction()
{
   //Remember you used grid name = "Grid"  so in the lines below replace ->
   //"GridName" with whatever grid name you assign   

   //read will request the server and reload only reload datasource
   $('#GridName').data('kendoGrid').dataSource.read();

   //refresh will re-render items in grid from the current datasource
   $('#GridName').data('kendoGrid').refresh();     
}
</script>