DevExpress MVC5 Grid中GetRowKey(e.visibleIndex)始终返回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 callingGetRowKey(), 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
Nameproperty of your server-side GridView matches what the client expects.Try
GetRowValues()as a Fallback: IfGetRowKey()still isn't working, you can directly fetch the primary key value usingGetRowValues()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

