DevExpress MVC GridView 刷新

Posted

技术标签:

【中文标题】DevExpress MVC GridView 刷新【英文标题】:DevExpress MVC GridView refresh 【发布时间】:2014-12-06 13:01:33 【问题描述】:

操作后需要刷新ajax成功函数中的DevExpress GridView。我正在使用gridName.Refresh() 方法,但 javascript 抛出“udefined is not a function”错误。但是当我将此方法写入 Google Chrome 控制台窗口时,它工作正常。问题是什么?此外,我的 javascript 代码位于不同的 JavaScript 文件中,而不是 html 代码中。

这是我在单独的 javascript 文件中的 jquery ajax 代码

function sendToMethod(url) 
    $.ajax(
        type: 'GET',
        url: url,
        contentType: 'application/html; charset=utf-8',
        datatype: 'html'
    )
    .success(function (result) 
        onCloseClick();
        gv_locations.UnselectRows();//gv_locations is not defined
        gv_locations.Refresh();//there is the same error here too
    )
    .error(function (result) 

    );

这是我的 GridViewPartial.cshtml 文件

@Html.DevExpress().GridView(g =>

    g.Name = "gv_locations";
    g.KeyFieldName = "PublicIP";
    g.SettingsPager.PageSize = 21;
    g.Width = System.Web.UI.WebControls.Unit.Percentage(100);
    g.ClientSideEvents.SelectionChanged = "onGridSelectionChanged";
    g.SettingsEditing.Mode = GridViewEditingMode.EditFormAndDisplayRow;

    g.CommandColumn.Visible = true;
    g.Settings.ShowGroupPanel = true;
    g.CommandColumn.ShowEditButton = true;
    g.CommandColumn.ShowDeleteButton = true;
    g.CommandColumn.ShowSelectCheckbox = true;
    g.SettingsBehavior.AllowFocusedRow = true;
    g.CallbackRouteValues = new  Controller = "Location", Action = "LocationGridViewPartial", id = ViewBag.Type ;

    g.SettingsEditing.UpdateRowRouteValues = new  Controller = "Location", Action = "LocationGridViewInlineUpdate" ;
    g.SettingsEditing.DeleteRowRouteValues = new  Controller = "Location", Action = "LocationGridViewInlineDelete" ;

    g.Columns.Add(column =>
    
        column.Caption = "Client";
        column.FieldName = "ClientID";
        column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.False;
    );

    g.Columns.Add(column =>
    
        column.Caption = "IP";
        column.FieldName = "PublicIP";
        column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.False;
    );

    g.Columns.Add("PublicIPName", "Lokasyon Adı");

    g.Columns.Add(column =>
    
        column.Caption = "Konum Tipi";
        column.FieldName = "SelectedLocationTypeID";
        column.ColumnType = MVCxGridViewColumnType.ComboBox;
        var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
        comboBoxProperties.DataSource = AtomicAdminPanel.Models.External.Location.LocationDataProvider.GetLocationTypes();
        comboBoxProperties.TextField = "SelectedLocationTypeName";
        comboBoxProperties.ValueField = "SelectedLocationTypeID";
    );

    g.Columns.Add(column =>
    
        column.Caption = "Oluşturan";
        column.FieldName = "CreatedUser";
        column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.False;
    );

    g.Columns.Add(column =>
    
        column.Caption = "Oluşturma Tarihi";
        column.FieldName = "CreatedDateTime";
        column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.False;
    );

    g.Columns.Add(column =>
    
        column.Caption = "Değiştiren";
        column.FieldName = "ChangedUser";
        column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.False;
    );

    g.Columns.Add(column =>
    
        column.Caption = "Değiştime Tarihi";
        column.FieldName = "ChangedDateTime";
        column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.False;
    );

    g.Columns.Add("IsActive", "Aktif", MVCxGridViewColumnType.CheckBox);

    MVCxGridViewColumn col_listType = new MVCxGridViewColumn("LocationListType");
    col_listType.Visible = false;

    if (ViewBag.Id == "Online")
    
        MVCxGridViewColumn col_online = new MVCxGridViewColumn("IsOnline", "Çevrimiçi", MVCxGridViewColumnType.Image);
        col_online.SetDataItemTemplateContent(c =>
        
            ViewContext.Writer.Write("<img src='../../Content/Image/statusOnline.png'/>");
        );
        g.Columns.Add(col_online);
    
).Bind(Model).GetHtml()

【问题讨论】:

我认为你应该显示一些代码。 我写了我的代码作为答案。因为我的代码太长了不适合这里:) 错误提示 gv_locations 未定义。 javascript 文件是否与 GridView 在同一页面上? 不是设计时间。因为 Devexpress 说“包含 gridview 的页面不包含其他 html 或 javascript 代码。您必须将包含 GridView 的页面分开。”同一 javascript 文件中的其他函数在 GridView 上运行良好。 我的意思是在浏览器的页面上,而不是网格所在的部分视图。您的意思是对于相同的gv_locations GridView,这些功能可以正常工作吗? 【参考方案1】:

首先你应该写window.gv_locations然后确保包含js脚本的视图看到这个网格(视图应该包含网格和脚本)。如果可行,请给我回复。

【讨论】:

如果gv_locations未定义,则以window.gv_locations访问时仍为未定义。

以上是关于DevExpress MVC GridView 刷新的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Devexpress MVC GridView 上获取选定的行?

基于所选行的 Devexpress MVC Gridview 自定义排序

devexpress gridview 中选择多行 剪切,粘帖功能怎么做?

devexpress gridview 中选择多行 剪切,粘帖功能怎么做?

devexpress GridView按条件给行号上色

DevExpress TreeList GridView 样式设置