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

Binding winth Linq To Sql from Button Click

Hi,
I am new to using these controls and I am having some trouble binding my grid.  I am trying to use the following linq to sql but I can't get it to work.  I am trying to bind on the click of a button with some criteria from a textbox(not currently in the linq to sql but will be added once I get this working).

public ActionResult LeadsByStateMailDate()
{
     var dkContext = new DKDatabaseEntities();
     var result = (from m in dkContext.Mailings
                          join p in dkContext.Persons on m.ID equals p.MailingID
                          join a in dkContext.Addresses on p.ID equals a.PersonID
                          where m.NoMail != "T"
                          group a by new { a.State, m.MailDate } into gg
                          orderby gg.Key
                          select new DKDatabaseModelClass.GrossLeadsByStateModel { MailDate = gg.Key.MailDate.ToString(), State = gg.Key.State, LeadCount = gg.Count() }).ToList();
     ViewBag.datasource = result.ToList();
     return View();
}

What should the razor code look like?

Also, is there an example of a parent child grid that I could see where selecting a row in the parent filters the child grid.  For example, lets say I have a parent grid that holds States and when a user clicks on a specific state, the child grid only shows data for that state.

Thanks.

7 Replies

MS Mani Sankar Durai Syncfusion Team December 27, 2016 01:14 PM UTC

Hi Richard, 

Thanks for contacting Syncfusion support. 

We have analyzed your query and we have prepared a sample by binding the data using Linq to Sql that can be downloaded from the below link. 
In this sample we have bound the data for the grid by passing through ViewBag. 
Please refer the below code example. 
[Index.cshtml] 
public ActionResult Index() 
        { 
            var ds = new NorthwindDataContext(); 
            var query = from emp in ds.EmployeeViews select emp; 
            ViewBag.datasource = query.ToList(); 
            ViewBag.data = new NorthwindDataContext().OrdersViews.ToList(); 
            var DetailData = new NorthwindDataContext().OrdersViews.Where(ord => ord.EmployeeID == 1).Take(5).ToList(); 
            ViewBag.dataSource1 = DetailData; 
            return View();  
        } 

Also in this sample we have rendered the grid as Master details grid. ie. When clicking the row in parent grid, based on the user clicks on a specific state, the child grid shows data for that state
 
Please refer the online sample link: http://mvc.syncfusion.com/demos/web/grid/masterdetails 
Also there is another type by specifying the child grid related to parent grid which is called as Hierarchy grid. Hierarchical binding can be used to create the Grid with parent and child relation, this facilitate you to view the child records for a particular row by clicking on the Expander button present in first column of each grid row. This can be enabled by using ChildGrid and QueryString. 
Please refer the documentation link. 
 
Refer the documentation link for Linq to Sql 
Please let us know if you need further assistance. 

Regards, 
Manisankar Durai. 



RD Richard Dublon January 10, 2017 09:50 PM UTC

Hi,
This sample works well but it's not exactly what I am looking for.
What I need to be able to do is call a method in my controller from the row selected event to populate the detail grid with a different linq to sql statement than the master grid.
Is there a way to do that?  I am open to using angular or a different tech to get this to work in mvc as I really like your grid control.


MS Mani Sankar Durai Syncfusion Team January 11, 2017 11:12 AM UTC

Hi Richard, 

We have analyzed your query and we suggest you to use AJAX post in the rowSelected event to call the method in the controller. And by returning the data from the controller to client side we can bind it to the grid. 

Please refer the below code example. 
[Index.cshtml] 
@(Html.EJ().Grid<EmployeeView>("MasterGrid") 
                   .Datasource((IEnumerable<object>)ViewBag.datasource) 
        .Columns(col => 
        { 
            ... 
         }) 
        .ClientSideEvents(eve => { eve.RowSelected("rowSelected"); }) 
) 
<div class="label1"> 
    Detail Grid 
</div> 
@(Html.EJ().Grid<OrdersView>("DetailGrid") 
            .Datasource((IEnumerable<object>)ViewBag.dataSource1) 
        .Columns(col => 
        { 
                         ... 
 
        }) 
) 
 
<script type="text/javascript"> 
    $(function () { 
        window.rowSelected = function (args) { 
            var value = args; 
            $.ajax({ 
                type: "POST", 
                url: "/Home/Event", 
                success: function (data) { 
                    var employeeID = value.data.EmployeeID;     //get the EmployeeId value to filter the data 
                    var  datasource = data; 
                    var detaildata = ej.DataManager(datasource).executeLocal(ej.Query().where("EmployeeID", ej.FilterOperators.equal, employeeID, false).take(10)); 
                    var gridObj = $("#DetailGrid").ejGrid("instance"); 
                    gridObj.dataSource(ej.DataManager(detaildata.slice(0, 5))); 
                }, 
             }); 
         } 
    }); 
</script> 
 
[Controller.cs] 
public ActionResult Event() 
        { 
            var ds = new NorthwindDataContext(); 
            var data = from ord in ds.OrdersViews select ord; 
            ViewBag.datasource = data.ToList(); 
            return Json(data, JsonRequestBehavior.AllowGet); 
        } 

In the above code example, using AJAX post we have triggered the method in the controller and used Linq to Sql. From the controller we have passed the data to the client side. In the AJAX success position we have bind the data for the detail grid. 
We have also prepared a sample that can be downloaded from the below link. 

Also when using AJAX post you can pass the data to the server side and filter the required data in the server side itself. 

Refer the documentation link of about rowSelected event in grid. 

Please let us know if you need further assistance. 

Regards, 
Manisankar Durai. 



RD Richard Dublon January 11, 2017 01:13 PM UTC

Amazing!!! 
Thank you so much for the help.  I have to say that I am very impressed with the level of support and the speed in which you give it.



MS Mani Sankar Durai Syncfusion Team January 12, 2017 12:46 PM UTC

Hi Richard, 

We are happy to hear that your problem has been solved. 

Please let us know if you need further assistance. 

Regards, 
Manisankar Durai. 



RD Richard Dublon January 18, 2017 02:37 PM UTC

I have another question...

What is the syntax to add a second field to my where clause(in bold below)?  I want it to filter based on state and date.

 $.ajax({
                                type: "POST",
                                url: "/Reports/LeadsByStateCountyMailDate",
                                success: function (data) {
                                    var state = value.data.State;     //get the EmployeeId value to filter the data
                                    var datasource = data;
                                    var detaildata = ej.DataManager(data).executeLocal(ej.Query().where("State", ej.FilterOperators.equal, state, false));
                                    var gridObj = $("#DetailGrid").ejGrid("instance");
                                    gridObj.dataSource(ej.DataManager(detaildata));


MS Mani Sankar Durai Syncfusion Team January 19, 2017 12:48 PM UTC

Hi Richard, 

Based on your query we have prepared a sample that can be downloaded from the below link. 
Link:                                                                                     http://www.syncfusion.com/downloads/support/forum/128000/ze/Sample7241644391594053781 

In this sample we have filtered based on two field for the master detail grid i.e. based on EmployeeID and CustomerID column in grid. 
Please refer the below code example. 
[Index.cshtml] 
    success: function (data) { 
                    var employeeID = value.data.EmployeeID; 
                    var  datasource = data; 
                    var detaildata = ej.DataManager(datasource).executeLocal(ej.Query().where(ej.Predicate('EmployeeID', ej.FilterOperators.equal, 1, true).and('CustomerID', ej.FilterOperators.startsWith, 'V', true)).take(5)); 
                    var gridObj = $("#DetailGrid").ejGrid("instance"); 
                    gridObj.dataSource(ej.DataManager(detaildata.slice(0, 5))); 
                }, 
             }); 

We can achieve this using and predicate as like we mentioned in the above code example. The “and” predicate is used to add n-number of predicates with “and” condition and filter the data. 

Also please refer the documentation link. 

Please let us know if you need further assistance. 

Regards, 
Manisankar Durai. 


Loader.
Up arrow icon