We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Get data for grid based on drop down value

Hi Team,

Am new to MVC. 
My target is show data in grid based on the drop down list.
am able to show the list of data in drop down from one table(Source table 1) successfully.
but am struck in how to display grid data based drop down value from second table(Source table 2).
Thanks for the advance.

Regards,
Sateesh

9 Replies

PK Prasanna Kumar Viswanathan Syncfusion Team July 10, 2017 12:16 PM UTC

Hi Sateesh, 

Thanks for contacting Syncfusion support. 

Before we proceed with your requirement, we need some clarifications. 

1. According to your requirement you need to bind data for grid based on dropdownlist value. So, please share the code example that you have used in your sample.  

2. Are you binding the data for the grid from the same table.  

3. You have mentioned that you have face issue while binding data from the second table(Source table 2). Here Source table 2 is a foreign-key table or different table. 
 
4. Do you face any script error while displaying grid data from the second table(Source table 2). If yes, share the screenshot.  
 
5. Essential Studio Version details. 
 
We have created a sample with dropdownlist and send postback to server on dropdown change event. In server-side we filter data based on dropdown value and return the data. In client-side we bind the data to the grid using dataSource method of ejGrid. 


Regards, 
Prasanna Kumar N.S.V 
 



SK sateesh kumar October 28, 2017 09:17 AM UTC

Dear  Prasanna Kumar,

Please find the attached my project.

In the project am able to assign the data to drop down(table1-States), and grid(table2-Cities).

Based on drop down value i want to change grid data. table 1 and table 2 relation with primary key and foreigen keys.

i used code first approach .

Regards,

J.Sateesh Kumar



Attachment: Cascade_Grid_e6dbd82b.7z


VN Vignesh Natarajan Syncfusion Team October 30, 2017 05:09 PM UTC

Hi Sateesh Kumar, 

Thanks for using Syncfusion products. 

We have analyzed your query and we suspect that you want to change the second grid data based on the dropDownList value. We have prepared a sample which can be downloaded from below link 


Refer the below code snippet. 

@Html.EJ().DropDownList("States").Datasource((IEnumerable<object>)ViewBag.state).DropDownListFields(df => df.Text("text")).Width("400").ClientSideEvents(eve => eve.Change("drpvaluechange")) 
 
@(Html.EJ().Grid<object>("Flat") 
 . . . . . . .  
 
        })) 
 
 
<script> 
    function drpvaluechange(args) { 
    
        $.ajax({ 
            url: "/Grid/Dropdown", 
            type: "POST", 
            dataType: "json", 
            data: { "country": args.text }, 
            success: function (data) { 
                var grid = $("#Flat").ejGrid("instance"); 
                grid.dataSource(data); 
            } 
        }); 
    } 
</script> 
 

Note: In the Change event of the DropDownList, ajax post is sent to server with the text and based on that data is filtered and On ajax success the second Grid data source is updated using dataSource method of the grid. 
Refer our online documentation for your reference    

Regards, 
Vignesh Natarajan  



SK sateesh kumar November 6, 2017 01:49 PM UTC

Dear Vignesh,

Thank you Vignesh for your help. 

using java script able to call the controller and getting the filter data. but m not able to refresh the grid with new data.

In view-

@Html.EJ().DropDownList("StateId").Datasource((IEnumerable<object>)ViewBag.StateDs).DropDownListFields(df => df.Text("StateName").Value("StateId")).Width("200").ClientSideEvents(eve => eve.Change("drpvaluechange"))


  @(Html.EJ().Grid<object>("Flat")

            .Datasource((IEnumerable<object>)ViewBag.CityDs)

.EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); })

     .AllowSorting().AllowPaging().PageSettings(page=>page.PageSize(5)).IsResponsive()

             .ToolbarSettings(toolbar =>

            {

                toolbar.ShowToolbar().ToolbarItems(items =>

                {

                    items.AddTool(ToolBarItems.Add);

                    items.AddTool(ToolBarItems.Edit);

                    items.AddTool(ToolBarItems.Delete);

                    items.AddTool(ToolBarItems.Update);

                    items.AddTool(ToolBarItems.Cancel);

                });

            }).Columns(col =>

     {

         col.Field("CityId").HeaderText("CityId").IsPrimaryKey(true).TextAlign(TextAlign.Left).Width("8%").AllowEditing().Add();

         col.Field("StateId").HeaderText("StateId").TextAlign(TextAlign.Left).Priority(4).Width("15%").AllowEditing().Add();

         col.Field("CityName").HeaderText("CityName").Width("15%").AllowEditing().Add();

         col.Field("CityState").HeaderText("CityState").TextAlign(TextAlign.Left).Priority(4).Width("15%").AllowEditing().Add();


     }))

 function drpvaluechange(args) {

        debugger;

        $.ajax({

            url: "/School/GetCities",

            type: "POST",

            dataType: "json",

            data: { "StateId": args.selectedValue },

            success: function (data) {

                var grid = $("#Flat").ejGrid("instance");

                grid.dataSource(data1);

            },

            error: function () {

                alert('error');

            }

        });

    }


Regards

J. Sateesh Kumar




VN Vignesh Natarajan Syncfusion Team November 7, 2017 11:40 AM UTC

Hi Sateesh Kumar, 

Thanks for the update. 

In your code example we found that you have defined the wrong value in grid dataSource method which is the root cause of an issue.  

Please refer the below codes 

function drpvaluechange(args) { 
        debugger; 
        $.ajax({ 
            url: "/School/GetCities", 
            type: "POST", 
            dataType: "json", 
            data: { "StateId": args.selectedValue }, 
            success: function (data) { 
                var grid = $("#Flat").ejGrid("instance"); 
                grid.dataSource(data1); 
            }, 
            error: function () { 
                alert('error'); 
            } 
        }); 
    } 


So modify the code as below snippet 

            success: function (data) { 
                var grid = $("#Flat").ejGrid("instance"); 
                grid.dataSource(data); 
            }, 
 


If the issue still exist please provide the following details 

1. Grid Controller code example.  

2. If possible, replicate the issue in the attached sample.  

3. Essential Studio Version details.  

Regards, 
Vignesh Natarajan 




SK sateesh kumar November 7, 2017 02:43 PM UTC

Dear Vignesh,

am using visual studio 2013. for this project we are using .netframework 4.5

please find the attached files.

Thansk for advance,

J Sateesh Kumar




Attachment: Refresh_Grid_based_on_Drop_Down__a36630a3.7z


VN Vignesh Natarajan Syncfusion Team November 8, 2017 12:39 PM UTC

Hi Sateesh Kumar, 

We have analyzed your two different types of sample. In type 1, you have updated dataSource in controller page by changing the Viewbag data by calling Ajax post. But it is not the correct approach. You have to update the DataSource on Ajax success by returning the data in form of Json. 

Please refer below codes 

Cshtml 

function drpvaluechange(args) { 
    
        $.ajax({ 
            url: "/Grid/Dropdown", 
            type: "POST", 
            dataType: "json", 
            data: { "country": args.text }, 
            success: function (data) { 
                var grid = $("#Flat").ejGrid("instance"); 
                grid.dataSource(data); 
            } 
        }); 
    } 

C# 

public ActionResult Dropdown(string country) 
        { 
            var employeeID = OrderRepository.GetAllRecords().Where(c => c.ShipCountry == country).ToList(); 
 
            return Json(employeeID, JsonRequestBehavior.AllowGet); 
        } 

Note: In Ajax post, we have passed the selected value of dropdownlist and filter the data in controller side and return the data in form of JSON and on ajax success, we have updated the Grid dataSource using dataSource method.  

For your convenience we have prepared a sample which can be downloaded from below link 

Regards, 
Vignesh Natarajan 



SK sateesh kumar November 9, 2017 05:38 AM UTC

Hi Vignesh,

as you said am passed the json from the controller only. but still am not able to refresh the grid with new data.

Note: Initially my ej grid load the all the cities . now am trying to change the grid when user select any one from the drop down.

Regards,

J. Sateesh Kumar




VN Vignesh Natarajan Syncfusion Team November 10, 2017 12:57 PM UTC

Hi Sateesh Kumar, 

Based on your suggestion we have prepared a sample which can be downloaded form below link 


Note: Initially Grid will bounded will all the States value and it dataSource will be updated based on the dropdown select value. 

Kindly share the following details to help you better 

  1. Share the full Grid rendering code(cshtml and controller page)
 
  1. Share the screenshot of console window for script error (if any)
 
 
  1. Share the screenshot of the network tab(Response page) when the post is send from the clientside to server
 
  1. And also share the network tab(Headers page) on Ajax success.
 
  1. If possible try to reproduce the issue in the provided sample
 
  1. Or share the issue producible sample
 
Regards, 
Vignesh Natarajan  



Loader.
Up arrow icon