You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DevExpress MVC5 Grid中GetRowKey(e.visibleIndex)始终返回null求助

Troubleshooting: MVCxClientGridView GetRowKey Returns Null

Hey there! Let's walk through why GetRowKey(e.visibleIndex) might be returning null for your GridView, and fix it step by step.

Most Common Cause: Missing KeyFieldName Configuration

First and foremost, the server-side GridView control must have the KeyFieldName property set—this is what the client-side GetRowKey() method relies on to fetch the primary key value. If this isn't configured, the method will always return null.

Here's how to set it in your Razor view:

@Html.DevExpress().GridView(settings => {
    settings.Name = "YourGridViewInstanceName"; // Make sure this matches your client-side reference
    settings.KeyFieldName = "YourPrimaryKeyColumn"; // Replace with your actual PK column name (e.g., "FundID")
    settings.ClientSideEvents.RowClick = "OnRowClick"; // Your click event handler
    // Other GridView configuration...
}).Bind(Model).GetHtml()

Additional Troubleshooting Steps

If KeyFieldName is already set, check these next:

  • Validate e.visibleIndex: Before calling GetRowKey(), log the visible index to confirm it's a valid, non-negative number that corresponds to an existing row:

    console.log("Visible Index:", e.visibleIndex);
    

    If the index is out of bounds (e.g., -1), that means the click event might be firing on a non-row element, or the row doesn't exist in the current GridView state.

  • Confirm Client-Side Grid Instance: Ensure MVCxClientGridView.Cast(s) is correctly returning your GridView's client-side object. Log the grid instance to verify:

    var grid = MVCxClientGridView.Cast(s);
    console.log("Grid Instance:", grid);
    

    If this returns undefined or an unexpected object, double-check that the Name property of your server-side GridView matches what the client expects.

  • Try GetRowValues() as a Fallback: If GetRowKey() still isn't working, you can directly fetch the primary key value using GetRowValues() instead, which targets the column explicitly:

    var key = grid.GetRowValues(e.visibleIndex, "YourPrimaryKeyColumn"); // Use your PK column name
    

Modified Client-Side Code Example

Here's an updated version of your script with these checks included:

function OnRowClick(s, e) {
    var grid = MVCxClientGridView.Cast(s);
    console.log("Visible Index:", e.visibleIndex);
    
    var key = grid.GetRowKey(e.visibleIndex);
    console.log("Row Key from GetRowKey:", key);
    
    // Fallback to GetRowValues if GetRowKey returns null
    if (!key) {
        key = grid.GetRowValues(e.visibleIndex, "FundID"); // Replace with your PK column
        console.log("Row Key from GetRowValues:", key);
    }
    
    $.ajax({
        url: '@Url.Action("FundDetails", "Fund")',
        type: "POST",
        dataType: "text",
        traditional: true,
        data: { rowKey: key },
        success: function (data) {
            console.log("Success Response:", data);
        },
        error: function (xhr, textStatus, errorThrown) {
            console.error("AJAX Error:", textStatus, errorThrown);
        }
    });
}

Start with verifying the KeyFieldName setting—that's the fix for 90% of cases where GetRowKey() returns null. If that doesn't resolve it, work through the other checks to narrow down the issue.

内容的提问来源于stack exchange,提问作者Haytham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:35:28