1

I am creating a asp.net mvc application and loaded the data into jqgrid and have paging and sorting working completely. I am trying to implement searching and have implemented code to display the search window; but when I click the Find button, I am unable to retrieve the searchString, searchField and searchOper since they are returned as empty. I am sure that I need to implement postdata code in the javascript but having trouble implementing one. Can anyone point me in the right direction?

Also, any idea about how to implement searching in the controller action??

This is what I have currently in the javascript:

 <script type="text/javascript">
    $(function () {
          $("#list").jqGrid({
              url: '/Home/GetData/',
                    datatype: 'json',
                    mtype: 'GET',
                    colNames: ['ID', 'NAME'],
                    colModel: [
      { name: 'ID', index: 'ID', width: 250, align: 'center', searchoptions: { sopt: ['eq', 'ne', 'cn']} },
      { name: 'NAME', index: 'NAME', width: 250, align: 'center',  searchoptions: { sopt: ['eq', 'ne', 'cn']} }],
                    pager: jQuery('#pager'),
                    rowNum: 10,
                    rowList: [5, 10, 20, 30, 40, 50],
                    sortname: 'ID',
                    sortorder: "desc",
                    viewrecords: true,
                    height: '100%'
                    });

                $("#list").jqGrid('navGrid', '#pager', { edit: true, add: true, del: true, search: true},
                                                        {},
                                                        {},
                                                        {},
                                                        {closeOnEscape: true, multipleSearch: true, closeAfterSearch: true},
                                                        {});


            }); 
</script>

Any help is greatly appreciated!

inspiringmyself
  • 590
  • 1
  • 11
  • 29

1 Answers1

2

You use multipleSearch: true searching option. It allows to create more powerful queries, but it uses another format of parameters. Instead of three parameters searchString, searchField and searchOper will be used one filters parameter which represent in form of JSON string the full information about the filter. See the documentation for more information.

In the answer for example you will find the code which demonstrate how one can parse the filters parameter and create the corresponding filtering of the data in case of usage Entity Framework for access to the database.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thank you very much for your help. I am following your link to implement controller action. When calling FilterObjectSet(DBcontext), I am passing a db context and that didn't work so I tried to convert it into ObjectSet and pass it as ObjectSet. This is what I did. ObjectContext objectContext = ((IObjectContextAdapter)db).ObjectContext; ObjectSet objectSet = objectContext.CreateObjectSet(); But still cannot get it work. Do you have any workaround for this issue? – inspiringmyself Jun 28 '12 at 17:56
  • @user1484365: The parameter of FilterObjectSet should be some class from `DBcontext` like `context.Questions` and not `DBcontext`. Are you sure that you use Entity Framework and not LINQ to SQL? You can download full working demo from [here](http://www.ok-soft-gmbh.com/jqGrid/jqGridDemoVS2010.zip) or [here](http://www.ok-soft-gmbh.com/jqGrid/jqGridExportToExcel.zip). I think that having working demo project you can modify it to your requirements. – Oleg Jun 28 '12 at 18:15
  • Yes, when calling FilterObjectSet(db.Response) I am passing the class in the context, but the issue is that DbContext class in Entity framework produces DBQuery instead of Object Query. So, when I pass FilterObjectSet(db.Response), it complains it cannot convert from DbSet to ObjectQuery, because Response is a DbSet in DbContext class. So, I am trying to figure out a way to convert DbQuery to ObjectQuery so that I can call the method appropriately. And yes, Iam using Entity Framework. – inspiringmyself Jun 28 '12 at 18:34
  • @user1484365: You can't use LINQ to Entities as input for `FilterObjectSet`. You should use the original `ObjectSet` like it should be defined in `DbContext` derived from `ObjectContext`. – Oleg Jun 28 '12 at 18:42
  • Actually in Code First, context classes inherit from DbContext class in System.Data.Entity and I cannot really inherit from ObjectContext. Does that mean, I cannot use your implementation at all? – inspiringmyself Jun 28 '12 at 19:15
  • @user1484365: Do you can use something like `DBcontext.CreateObjectSet("Response")` to get `ObjectSet`? – Oleg Jun 28 '12 at 19:46
  • @user1484365: If you will have no success you could have to use [Dynamic Linq Framework](http://sourceforge.net/projects/dynamiclinq/) see [here](http://nuget.org/packages/DynamicLINQ). – Oleg Jun 28 '12 at 19:51
  • Yes, I tried that. ObjectContext objectContext = ((IObjectContextAdapter)db).ObjectContext; (where db is the context).ObjectSet objectSet = objectContext.CreateObjectSet("RequestResponse"). Then tried calling FilterObjectSet(objectSet) and it complains "No implicit conversion between System.Data.Entity.DbSet to System.Data.Objects.ObjectQuery". I am confused as to how it assumes "objectSet" as DbSet even after converting to objectContext and then ObjectSet. – inspiringmyself Jun 28 '12 at 19:53
  • @user1484365: I am not sure what is the problem in your EF model. I can use `ObjectSet questions = context.CreateObjectSet("Questions");` and then use `ObjectQuery filteredQuery = f.FilterObjectSet(questions);`. I am not sure why you can't do the same. Probably you should use `"RequestResponses"` instead of `"RequestResponse"`? What is the name of the table in the database? – Oleg Jun 28 '12 at 21:21
  • I realized what the issue was. When calling this statement, ObjectQuery filteredQuery = (f == null ? objectSet : f.FilterObjectSet(objectSet)); objectSet should be placed in both the places; but what I did was ObjectQuery filteredQuery = (f == null ? db.RequestResponses : f.FilterObjectSet(objectSet)); and didn't realize the error was because of db.RequestResponses since I was just concentrating on FilterObjectSet method. I really want to appreciate your time and responsiveness Oleg, you are awesome! – inspiringmyself Jun 28 '12 at 22:18
  • Sorry Oleg, I was not allowed to upvote since it needs fifteen reputation and I am really new to stack overflow! Anyway, Thank you once again. – inspiringmyself Jun 28 '12 at 22:21
  • @user1484365: Upvoting is not a problem. You will get the right after 15 reputation points. I have enough reputation points which I'll never use. :-) The most important that you have now a working solution and I'm glad that I could help you. Best wishes! – Oleg Jun 28 '12 at 22:26
  • Thanks. But I think I will have more questions for you in the future as I implement more functionality, so keep an eye on the post and keep up your good work :) – inspiringmyself Jun 28 '12 at 22:32