我的控制器在应用程序启动时加载结果部分视图“SOMETIMES”

时间:2015-03-14 00:47:01

标签: c# jquery ajax asp.net-mvc asp.net-mvc-4

我有一个主页,它有一个.CSHTML视图,当我们点击Grid元素时,它最终会调用Partial视图。但是,有时当我启动我的应用程序时,它会首先尝试加载部分视图。我不知道为什么会这样?

只有在单击Grid上的元素时才必须尝试加载局部视图,但是这里它没有单击就调用了局部视图。关注我的代码片段:

!DOCTYPE html>
@using (Html.BeginForm())
{
    <div id="clientsDb">
        @(Html.Kendo().Grid<Employee>()
              .Name("employeeGrid")
              .Columns(columns =>
              {
                  columns.Bound(c => c.Id).Width(140).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains")));
                  columns.Bound(c => c.FirstName).Width(500).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains")));
                  columns.Bound(c => c.LastName);
              })
              .HtmlAttributes(new {style = "height: 380px;"})
              .Scrollable()
              .Groupable()
              .Sortable()
              .Selectable()
              .Pageable(pageable => pageable
                  .Refresh(true)
                  .PageSizes(true)
                  .ButtonCount(5))
              .Filterable(filterable => filterable
                  .Extra(true)
                  .Operators(operators => operators
                      .ForString(str => str.Clear()
                          .Contains("Contains")
                          .IsEqualTo("Exactly matches")
                          .StartsWith("Starts with")
                          .DoesNotContain("Does not contain")
                          .EndsWith("Ends with")
                          .IsNotEqualTo("Is not equal to")
                      ))).DataSource(dataSource => dataSource
                          .Ajax()
                          .Read(read => read.Action("ReadEmployee", "Employee"))))
    </div>

    <script type="text/javascript">
        $("#employeeGrid").click(function() {
            var grid = $("#employeeGrid").data("kendoGrid");
            var currentSelection = grid.dataItem(grid.select());
            $.ajax({
                data: { id: currentSelection.Id },
                url: "/Employee/LoadTabControl/" +  currentSelection.Id,
                type: "POST",
                success: function (result) {
                    $('#EmployeeDetails').html(result);
                }
            });
        });
    </script>

    <div id ="EmployeeDetails"></div>

正如你所看到的那样“/ / Employee / LoadTabControl /”或者除非用户点击GRID,否则不应该调用它加载的任何东西。但是,当我启动应用程序时,应用程序正试图“有时”加载它有什么建议吗?

1 个答案:

答案 0 :(得分:2)

<script type="text/javascript">
$(document).ready(function(){
 var grid = $("#employeeGrid").data("kendoGrid");


$("#employeeGrid").click(function() {
            var currentSelection = grid.dataItem(grid.select());
            $.ajax({
                data: { id: currentSelection.Id },
                url: "/Employee/LoadTabControl/" +  currentSelection.Id,
                type: "POST",
                success: function (result) {
                    $('#EmployeeDetails').html(result);
                }
            });
        });

});

    </script>