Page 1 of 1

Using jquery Ajax

Posted: Wed Dec 28, 2016 6:24 am
by Amrutha
Dear Team,

Is it possible to use jquery.ajax() using the inbuilt ExecuteJavaScriptAsync() so that value can be returned to the c# code ?

If possible then kindly share some sample code for the same.

Thanks & Regards,
-A

Re: Using jquery Ajax

Posted: Sat Dec 31, 2016 6:09 am
by JS-Support @Userware
Dear Amrutha,

Sure! Please find the sample code at:

http://forums.cshtml5.com/viewtopic.php?f=7&t=7900

Regards,
JS-Support

Re: Using jquery Ajax

Posted: Mon Jan 02, 2017 6:08 am
by Amrutha
Hello Team,

Thank you for the new feature.

But I want to call my local function i.e from my project, Is it possible?

I tried to access it (eg : http://localhost ) but was not able.

Example :
In the given example for jquery.Ajax() it is calling a service , but i need to invoke a function/method from my silverlight/Cshtml5 project.
Like should be able to capture the data from a rowselected event of a grid.


Somewhere I think I am not calling it properly.

Please let me know where I need to correct by code.

Kind Regards,
Amrutha

Re: Using jquery Ajax

Posted: Mon Jan 02, 2017 6:46 am
by JS-Support @Userware
Hello Amrutha,

To call a local function in your project from within that very same project (ie. client-side to client-side communication), you shouldn't use "jQuery.ajax", which is more suited for client/server communications.

What grid component are you using? If you are using the built-in "DataGrid" control, you can hook the C# event "SelectionChanged", and then access the selected row by accessing the property "DataGrid.SelectedItem".

Thanks.
Regards,
JS-Support

Re: Using jquery Ajax

Posted: Tue Jan 03, 2017 11:30 pm
by Amrutha
Hello Team,

I was trying to implement Kendo UI Grid.

The client side change event is working but I need to get the selected rows value to C# side.

I was referring to this link for Kendo UI change/select event : http://dojo.telerik.com/AzOPu

Thanks & Regards,
A.

Re: Using jquery Ajax

Posted: Thu Jan 05, 2017 10:23 am
by JS-Support @Userware
Hello Amrutha,

Oh ok, I see.

Yes, this is possible. All you have to do is pass a C# callback to the "Interop.ExecuteJavaScript" method, so that the C# callback gets called when the JavaScript "change" event happens.

Here is an example, using the code snippet that you sent me:

Code: Select all

CSHTML5.Interop.ExecuteJavaScript(@"
    $(""#grid"").kendoGrid({
      columns: [
        { field: ""name"" },
        { field: ""age"" }
      ],
      dataSource: [
        { name: ""Jane Doe"", age: 30 },
        { name: ""John Doe"", age: 33 }
      ],
      selectable: ""row"",
      change: function(e) {
        var selectedRows = this.select();
   
        var selectedDataItems = [];
        for (var i = 0; i < selectedRows.length; i++) {
          var dataItem = this.dataItem(selectedRows[i]);

          //------------------------
          // Let's call the C# callback:
          //------------------------
          $0(dataItem.name); // '$0' here means the first argument of the 'Interop.ExecuteJavaScript' method.

          selectedDataItems.push(dataItem);
        }
      }
    });", (Action<string>)OnRowSelected);


and then you define the callback somewhere in your C# class:

Code: Select all

public void OnRowSelected(string name)
{
    System.Windows.MessageBox.Show("You selected: " + name);
}


By the way, we expect to have a full working demo of the Kendo UI Datagrid component before the end of February. We are working on a "Kendo UI" extension for CSHTML5 that will take advantage of the upcoming feature that lets you import TypeScript Definition files.

Regards,
JS-Support

Re: Using jquery Ajax

Posted: Tue Jan 10, 2017 5:07 am
by Amrutha
Thank you Team for your reply.

I will try to implement this.

Thanks and Regards,
Amrutha