tags:

views:

203

answers:

2

I have a grid (foreach in view) which is shown based on a GET request.

For the POST request, I want to return a filtered view of the grid. The grid is already a partial view, so just returning the grid is no problem.

However, I am looking for some sample code on how I get my filter conditions (there are quite a few, I would have those selected clientside via dropdowns) back to the controller's POST request.

I'd really appreciate some sample code, client & server side using jQuery as the Javascript library for the client side code.

Thank you!

+2  A: 

I write code like this.

var url = '<%= Url.Action("List", new { controller = "ControllerName" }) %>';
$.post(url,
    $("#criteria_form").serialize(),
    function(data) {
      $("#list_holder").html(data);
    }
);
Craig
Could you please explain your code a bit more detailed, and also show the C# side? Thank you!
Alex
There is nothing more really. The C# just renders a View which is a user control and it gets returned as the data parameter. The criteria are passed as parameters to the action.
Craig
How do you deserialize the data parameters in the action?Meaning, how does the signature look like: public ActionResult Filter(?????) and how do you access the individual serialized items from the criteria_form?
Alex
They will be deserialized automagically. So if you have a criteria field called SearchText then have a parameter SearchText and it will pick it up.
Craig
I still dont see it. Please tell me how I will bring the content of the field SearchText into a string inside of my controller method.String searchText = ???
Alex
When you call the $("#").serialize() it will create the data to be passed to the controller which is then mapped to the Action parameters. How does it work? It doesn't really matter as it is all handled by MVC framework. Quote:"The ASP.NET MVC framework can automatically map URL parameter values to parameter values for action methods. By default, if an action method takes a parameter, the MVC framework examines incoming request data and determines whether the request contains an HTTP request value with the same name. If so, the request value is automatically passed to the action method"
Craig
+1  A: 

The C# part would look like this, if you use Craig's example, note that the action's arguments need to have the same name as in the html search criteria form !

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(string searchtext)
{
     // retrieve data here based on searchtext


     //return partial view to be used in the grid
     return View("_partial", myDataCollection)
}

You can also look into jQuery addons like jqGrid or TableSorter.

Morph
Thanks for the links, jqGrid looks just like what I have been looking for!
Colin Desmond