在ASP MVC中更新没有页面重新加载的kendo网格列

时间:2015-11-25 18:00:29

标签: asp.net-mvc-4 razor grid kendo-grid

我有一个kendo网格,用户可以从网格中选择列列表,并通过提供名称(即视图名称)来保存选择。每个保存的选择(视图名称)将显示为网格上方的下拉列表,以便用户可以随时更改网格列。在当前实现中,每当用户从一个下拉值中选择视图名称到其他时,我调用action方法来选择该视图名称作为当前视图名称。然后页面重新加载以调用Index的操作方法来检索当前视图名称列值。我在网格中使用可见属性来显示和隐藏网格中的列。

现在我想知道当用户从下拉列表中更改视图名称时,是否可以在不重新加载页面的情况下更新网格列。

由于 Sanjeev

截图: enter image description here

这是我的设置: 的视图模型:

namespace MvcApplicatioin.Models
{
    public class EmployeeViewModel
    {
        public EmployeeColumns EmployeeColumns               { get; set; }
        public IEnumerable<SelectListItem> EmployeeViewNames { get; set; }
        public long EmployeeSelectedViewId                   { get; set; }
    }

    public class EmployeeResponse
    {
        public int Id           { get; set; }
        public string FirstName { get; set; }
        public string LastName  { get; set; }
    }

    public class EmployeeColumns
    {
        public bool Id        { get; set; }
        public bool FirstName { get; set; }
        public bool LastName  { get; set; }
    }
}

控制器:

public class EmployeeController : Controller
{
        // GET: Employee
        public ActionResult Index()
        {
            var service = new EmployeeService();
            EmployeeViewModel model = new EmployeeViewModel();
            long currentViewId;

            //setup views and column preferences
            EmployeeColumns employeeColumns = service.GetCurrentEmployeeColumnsPreferences();
            model.EmployeeColumns = employeeColumns;
            model.EmployeeViewNames = service.GetAllEmployeeViewNames(out currentViewId);
            model.EmployeeSelectedViewId = currentViewId;

            return View(model);
        }
 }

剃刀:

@using Kendo.Mvc.UI

@{
    ViewBag.Title = "Employee Info:";
}

<h3 style="margin-bottom: 10px;">Employee Info</h3>

<input id="btnSearch" type="button" value="Search" class="btn_Search" />

<div class="row">
    <div class="col-sm-12">
        @(Html.Kendo().Grid<MvcApplicatioin.Models.EmployeeResponse>()
            .Name("GridEmployee")
            .Columns(columns =>
            {
                columns.Bound(e => e.Id).Width("170px").Visible(Model.EmployeeColumns.Id);
                columns.Bound(e => e.FirstName).Width("190px").Visible(Model.EmployeeColumns.FirstName);
                columns.Bound(e => e.LastName).Width("170px").Visible(Model.EmployeeColumns.LastName);                
            })
            .ToolBar(tools =>
            {
                tools.Template(@<text>
                    <div class="col-lg-4 col-md-5 col-sm-5 col-xs-7 pull-right" style="padding-right: 0;">
                        <div class="form-group" style="margin-bottom: 0;">
                            @Html.Label("Grid View:", new { @class = "col-sm-3 col-xs-4 control-label view" })
                            <div class="col-sm-7 col-xs-6" style="padding-left: 0;">
                                @Html.DropDownList("lstEmployeeViewNames", new SelectList(Model.EmployeeViewNames, "Value", "Text", Model.EmployeeSelectedViewId), "- Select View Name -", new { @class = "form-control", @style = "height: auto;" })
                            </div>
                        </div>
                    </div>
                </text>);
            })
            .Pageable(x => x.PageSizes(new int[] { 10, 20, 50, 100 }).ButtonCount(4))
            .AutoBind(false)
            .DataSource(dataSource => dataSource
                        .Ajax()
                        .PageSize(10)
                        .ServerOperation(false)
                        .Read(read => read.Action("SearchEmployee", "Employee")))
        )
    </div>
</div><!--//row-->

<script type="text/javascript">
    $('#btnSearch').click(function (e) {
        e.preventDefault(); //This prevent the submit button onclick from submitting by itself

        $('#GridEmployee').data('kendoGrid').dataSource.read();
    });

    //Change event for Dropdown placed inside the Grid's Toolbar - To Change the view
    $("#lstEmployeeViewNames").change(function (e) {
        var selectedViewId = $('select#lstEmployeeViewNames option:selected').val();

        if (selectedViewId == null || selectedViewId == '') {
            alert("Please select the view name from the dropdown first !!");
            return;
        }

        $.post("/Employee/SetEmployeeColumnsCurrentPreferences", { viewId: selectedViewId }, function (data) {
            window.top.location.reload();
        });
    });
</script>

1 个答案:

答案 0 :(得分:0)

这就是我在客户端进行搜索的方式。我知道它并没有说明如何显示和隐藏列,但至少它可以让你走上正确的轨道。

诀窍是操纵$(&#34;#GridEmployee&#34;)而不是发布。

function search() {
    var searchCriteria = $("#searchField").val();
    var gridData = $("#GridEmployee").data("kendoGrid");

    if searchCriteria != "") {
        gridData.dataSource.filter({ field: "FirstName", operator: "contains", value: searchCriteria });
    } else {
        gridData.dataSource.filter({});
    }
}