views:

57

answers:

1

Hi,

Autocomplete does not call corresponding action on Controller. In my case here SharedContoller\FindLocation does not get invoked on typing anything in the textbox of autocomplete. Rest all things are working fine.

Declaration of scripts :

<script src="../../Scripts/jquery-1.4.2.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-ui-1.8.custom.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.autocomplete.js" type="text/javascript"></script>    
<script src="../../Scripts/jquery.jqGrid.js" type="text/javascript"></script>
<script src="../../Scripts/js/jqModal.js" type="text/javascript"></script>
<script src="../../Scripts/js/jqDnR.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.maskedinput-1.2.2.min.js" type="text/javascript"></script>
<script src="../../Scripts/js/grid.locale-en.js" type="text/javascript"></script>

Table declaration :

<table id ="RegionAndCity" class="scroll" cellpadding="0" cellspacing="0"></table>
<div id="listPager" class="scroll" style="text-align:center;"></div>   

Finally the jqgrid and autocomplete code :

var PlanId = $("#PlanId").val();
var updateDialog = {
    url: '<%= Url.Action("UpdateRegionAndCity", "BriefAllocation") %>'
    , closeAfterAdd: true
    , closeAfterEdit: true
    , recreateForm: true
    , modal: false,
    onclickSubmit: function (params) {
        var ajaxData = {};
        var plannerName = $("#PlannerId :selected").text();
        ajaxData = { PlanId: PlanId, PlannerName: plannerName,
                     LocationName: $("#LocationId :selected").text() };
        return ajaxData;
    }
};       

$("#RegionAndCity").jqGrid({
    url: '/BriefAllocation/GetRegionAndCities/?PlanId=' + PlanId,
    datatype: 'json',
    mtype: 'GET',
    prmNames: { PlanId: PlanId },
    colNames: ['RegionsAndCitiesId', 'LocationId', 'LocationName'],
    colModel: [
        { name: 'RegionsAndCitiesId', index: 'RegionsAndCitiesId', width: 100,
          align: 'left', /* key: true,*/editable: true,
          editrules: { edithidden: false }, hidedlg: true, hidden: true },
        { name: 'LocationId', index: 'LocationId', width: 150, align: 'left',
          editable: false, edittype: 'select', editoptions: { value: countries },
          editrules: { required: true }, formoptions: { elmsuffix: ' *'} },
        { name: 'LocationName', index: 'LocationName', width: 300, align: 'left',
          editable: true, edittype: 'text',
          editoptions: { dataInit:
              function (elem) {
                  setTimeout(function () {
                      $(elem).autocomplete('/Shared/FindLocation/', {
                          dataType: "json",
                          multiple: false,
                          formatItem: function (item, index, total, query) {
                              return item.value;
                          },
                          parse: function (data) {
                              return $.map(data, function (item) {
                                  return {
                                      data: item,
                                      value: item.Key,
                                      result: item.value
                                  }
                              });
                          }
                      }).result(function (event, row) {
                          $("#LocationId").val(row.Key);
                      });
                  }, 100);
              }
          }, editrules: { required: true }
        }],
        pager: $('#listPager'),
        rowNum: 10,
        rowList: [5, 10, 20, 50],
        sortname: 'RegionsAndCitiesId',
        sortorder: "asc",
        viewrecords: true,
        imgpath: '/scripts/themes/steel/images',
        caption: '<b>Regions And Cities</b>',
        ondblClickRow: function (rowid, iRow, iCol, e) {
            $("#RegionAndCity").editGridRow(rowid, prmGridDialog);
        }
    }).navGrid('#listPager', { edit: true, add: true, del: true, refresh: true },
            updateDialog,
            updateDialog,
            updateDialog);
});

Autocomplete calls SharedController\FindLocation. Code for it is :

 public ActionResult FindLocation(string q, int limit)
    {
        return Content("");
    }

This method does not get invoked. I also tried with :

 public ActionResult FindLocation()
    {
        return Content("");
    }

This also does not get invoked. FindLocation with parameters works fine if autocomplete is used with a html textbox.

Pl help me find the problem. Saw almost all the posts related to this..working on this since 2 days now..

Thanks, Bhoomi.

A: 

First of all there are a simple but very important error in your program: the file grid.locale-en.js must be inserted before jquery.jqGrid.js. If you use Developer Version of jqGrid from GitHub then grid.locale-en.js is already loaded inside of jquery.jqGrid.js.

Other small syntax errors are in the updateDialog. In JavaScript you should be carefull with the new lines. In some situations you have to not insert ; at the end of line and line break insert it for you. For example you forget semicolon after

return {
    data: item,
    value: item.Key,
    result: item.value
}

it should be

return {
    data: item,
    value: item.Key,
    result: item.value
};

but it is not so bad. Onother place in not so good. You should rewrite updateDialog like following

var updateDialog = {
    url: '<%= Url.Action("UpdateRegionAndCity", "BriefAllocation") %>',
    closeAfterAdd: true,
    closeAfterEdit: true,
    recreateForm: true,
    modal: false,
    onclickSubmit: function (params) {
        return { PlanId: $("#PlanId").val(),
                 PlannerName: $("#PlannerId :selected").text(),
                 LocationName: $("#LocationId :selected").text() };
    }
};

It is important to place commas at the end of line and place var PlanId = $("#PlanId").val(); inside of onclickSubmit function. In your current code $("#PlanId").val() will be read outside of onclickSubmit, so you will use all time old value from "#PlanId" field.

Moreover which version of jqGrid you use? Attribute class="scroll" is noot needed since some versions of jqGrid. Moreover I recommend you use jQuery UI 1.8.2 together with jqGrid 1.7.2.

The usage some default option like align: 'left' or editable: false is not needed.

Oleg
Thank you Oleg.
Bhoomi
I was using a old version of jqgrid so I downloaded the new version from http://github.com/tonytomov/jqGrid.
Bhoomi
Sorry by mistake i clicked on Add Comment. I was using older version jqgrid so started with latest version. Table is not loading at all now. I took all the new .js files and referenced it. Tried removing AutoComplete code also but normal table also does not work now. Any clues? Does it work with ASP.NET MVC?
Bhoomi
@Bhoomi: Could you post the JSON data which you currently send from the control action back to the jqGrid? It you want that I help you just post enough data that I would be able to reproduce your problem. For example in your HTML fragment there are no elements with id PlanId, PlannerId and LocationId. Do you have LocataionId outside of jqGrid and incite? Can 'RegionsAndCitiesId' be ID of you rows? Which version of ASP.NET MVC and which version of Visual Studio you use?
Oleg
Thanks Oleg, My code is working correctly now, problem was that I was using older version and then jqgrid-en-lang was creating problem. grid.locale-en.js was to be placed before jqgrid.js. I did that the undefined error went away.. Thanks a lot..
Bhoomi
@Bhoomi: I am glad to hear it! Congratulations! If the problem is solved you can consider to accept the question (see http://meta.stackoverflow.com/questions/5234/accepting-answers-what-is-it-all-about and http://stackoverflow.com/faq#howtoask). If you will read some FAQs (see http://meta.stackoverflow.com/questions/7931/) you will understand and use stackoverflow more effective for you. Best regards and good luck!
Oleg