This is a migrated thread and some comments may be shown as answers.

[Solved] Grid client side rebind() not working without route values initially defined on grid

1 Answer 197 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
brandon
Top achievements
Rank 1
brandon asked on 17 Jan 2010, 02:01 AM
Assume you define the .Ajax() method on your grid without any routeValues (but your ActionMethod has a parameter defined).  If you call the client-side grid rebind() method and pass in routeValue arguments, they never get sent to the controller's actionmethod.  The reason seems to be that this javascript below is doing a regex replace so the new route value you specified in the client-side rebind() method doesn't get added
 for (var key in args) {
    var regExp = new RegExp($t.formatString('({0})=([^&]*)', key), 'g');
    this.ajaxUrl = this.ajaxUrl.replace(regExp, '$1=' + args[key]);
}

The problem is that now I am forced to specify my routeValues (searchCriteria) on the grid definition.  But the first time I load the grid, I don't have a value for searchCriteria.  The workaround (hack) that I found is to specify a blank space in the searchCriteria routeValue, since I can't pass in null or an empty string.  Then I do a C# 4 string.IsNullOrWhiteSpace check in the action method to make sure its not the blank space being sent it.

Details:
--------------------------------------------------------------------------------
My controller action method
[GridAction] 
public ActionResult MyActionMethod(string searchCriteria) 
{ 
    //pass a gridmodel to the view 
} 

My grid (with the hack of specify a blank space for the search criteria)
<%= Html.Telerik().Grid(Model) 
        .Ajax(ajax => ajax.Action("MyActionMethod", "MyController", new { searchCriteria = " " }))         
%> 

    
jQuery call to search the grid
var grid = $('#Grid').data('tGrid'); 
grid.rebind({searchCriteria: $(this).val()}); 

1 Answer, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 18 Jan 2010, 12:56 PM
Hi brandon,

You are right. I easily reproduced the problem and fixed it. To apply the fix please replace this
for (var key in args) {
    var regExp = new RegExp($t.formatString('({0})=([^&]*)', key), 'g');
    this.ajaxUrl = this.ajaxUrl.replace(regExp, '$1=' + args[key]);
}
with:
for (var key in args) {
    var regExp = new RegExp($t.formatString('({0})=([^&]*)', key), 'g');
    if (regExp.test(this.ajaxUrl))
        this.ajaxUrl = this.ajaxUrl.replace(regExp, '$1=' + args[key]);
    else {
        var url = new $t.stringBuilder();
        url.cat(this.ajaxUrl);
        if (this.ajaxUrl.indexOf('?') < 0)
            url.cat('?');
        this.ajaxUrl = url.cat(key).cat('=').cat(args[key]).string();
    }
}

Regards,
Atanas Korchev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
brandon
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Share this question
or