0

I am using jqgrid in asp.net MVC5 with Entity Framework. For one of the columns I require it to be populated via database as a dropdown.

$("#grid").jqGrid({
    colNames: ['Id', 'Date', 'PersonelID', 'OnCallType', 'Comments'],
            colModel: [
                { key: true, hidden: true, name: 'Id', index: 'Id', editable: true },

                { key: false, name: 'Date', index: 'Date', editable: true, formatter: 'date', formatoptions: { newformat: 'm/d/Y' }, sortable: true },
                { key: false, name: 'PersonelID', index: 'PersonelID', editable: true, edittype: 'select', editoptions: { dataUrl: '/Que/GetPersonnel' } },
                { key: false, name: 'OnCallType', index: 'OnCallType', editable: true, edittype: 'select', editoptions: { dataUrl: '/Que/GetCallType' } },
             { key: false, name: 'Comments', index: 'Comments', editable: true }, ]

});

what should i use in EditOptions to fetch value from database.

I tried using dataurl, but I couldnt get the code to work:-

 public string GetPersonnel()
        {
            string final=string.Empty;
           var query= new SelectList(db.PersonnelLocals.Where(o => o.QueLoc == true), "Index", "Text");
           foreach (var item in query)
           {
               string newItem=item.Value.ToString()+":"+item.Text.ToString();
               final = final + newItem + ",";
           }


           return final;
        }

If there is any better method than using dataurl. Any help is greatly appreciated.

anu
  • 305
  • 1
  • 5
  • 14
  • The method GetPersonnel() doesnt even get called when I put a breakpoint. I changed the url to dataUrl: '~/Queens/GetPersonnel', but that didnt help either. – anu Nov 02 '15 at 22:03
  • I will be busy one day, after that I would try to help you if the problem will still not solved. I recommend you 1) to use [Fiddler](http://www.telerik.com/fiddler) or Developer Tools of IE/Crome (press F12 and chose Network tab) to make HTTP trace. you will be see which URL with which data will be POST. 2) chande `dataUrl: '/Que/GetPersonnel'` to `dataUrl: '@Url.Action("Que", "GetPersonnel")'`, 3) change implementation of `GetPersonnel` action based on [the answer](http://stackoverflow.com/a/4102155/315935). The line `jQuery.parseJSON(data.responseText)` replaces to `data.responseText`. – Oleg Nov 02 '15 at 22:45
  • Thanks Oleg. am trying to follow this tutorial. http://www.codeproject.com/Articles/801895/ASP-NET-MVC-Entity-Framework-and-JQGrid-Demo-wit. Though I am able to hit functions now with the correct URL as dataUrl: 'Que/GetPersonnel', but in Fiddler I see JSOn=20007 in the response,also viewing in XML shows "Data at root level is invalid". My getPersonnel function returns String and I updated the parseJson to data.responseText as stated. But dropdowns not populated. Also the function is hit only when I click on Add and Edit button, so in display of gridview it is shown as blank. – anu Nov 03 '15 at 16:05

0 Answers0