Using jquery Ajax

Please post public support tickets here. Note: for private support tickets, please send an email to support@cshtml5.com instead.
Amrutha
Posts: 21
Joined: Tue Nov 15, 2016 5:10 am

Using jquery Ajax

Postby Amrutha » Wed Dec 28, 2016 6:24 am

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

JS-Support @Userware
Site Admin
Posts: 1142
Joined: Tue Apr 08, 2014 3:42 pm

Re: Using jquery Ajax

Postby JS-Support @Userware » Sat Dec 31, 2016 6:09 am

Dear Amrutha,

Sure! Please find the sample code at:

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

Regards,
JS-Support

Amrutha
Posts: 21
Joined: Tue Nov 15, 2016 5:10 am

Re: Using jquery Ajax

Postby Amrutha » Mon Jan 02, 2017 6:08 am

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

JS-Support @Userware
Site Admin
Posts: 1142
Joined: Tue Apr 08, 2014 3:42 pm

Re: Using jquery Ajax

Postby JS-Support @Userware » Mon Jan 02, 2017 6:46 am

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

Amrutha
Posts: 21
Joined: Tue Nov 15, 2016 5:10 am

Re: Using jquery Ajax

Postby Amrutha » Tue Jan 03, 2017 11:30 pm

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.

JS-Support @Userware
Site Admin
Posts: 1142
Joined: Tue Apr 08, 2014 3:42 pm

Re: Using jquery Ajax

Postby JS-Support @Userware » Thu Jan 05, 2017 10:23 am

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

Amrutha
Posts: 21
Joined: Tue Nov 15, 2016 5:10 am

Re: Using jquery Ajax

Postby Amrutha » Tue Jan 10, 2017 5:07 am

Thank you Team for your reply.

I will try to implement this.

Thanks and Regards,
Amrutha


Return to “Technical Support”

Who is online

Users browsing this forum: No registered users and 47 guests