Is it possible to access the grid in ASP.NET Kendoui through code?

When trying to access my grid from Javascript code, I seem to have made mistakes. Can you help me locate them?

This is the Grid code I am using:

 <div id="kendoo">
@(Html.Kendo().Grid<SalePortal.ServiceReference.Product>()
.Name("Grid")
.Columns(columns =>
    {
        columns.Bound(product => product.pID).Title("Product ID");
        columns.Bound(product => product.productName).Title("Product Name");
        columns.Bound(product => product.productPrice).Title("Price");
        columns.Command(command => command.Custom("Buy").Click("Sale"));
    })
    .DataSource(dataSource => dataSource
    .Ajax()
    .ServerOperation(false)
    .Read(read => read.Action("GetProduct","Home"))
    )

)
</div>

And here is my javascript snippet:

    <script type="text/javascript">

    function Sale(e)
    {
        var grid = $("#kendoo").data("kendoGrid");
        var myvar = grid.dataItem($(this).closest("tr"));
        alert(my.pID);


        var url = "@Url.Action("Sale", "Home")";
        $.ajax({
            url: url,
            type: 'POST',
            data: { cID: 1, pID: prID },
        });
     }
</script>

Upon runningthe website, the variable grid in the Javascript section appears as "undefined". This leads to an error message:

"Javascript runtime error: dataItem of undefined or null reference"

It seems that due to the undefined status of the grid, this error arises. What steps can be taken to rectify this issue so I can successfully access the selected row cell?

Answer №1

Here is a suggested solution:

<script type="text/javascript">

function SellItem(e)
{
    var grid = $("#Grid").data("kendoGrid"); // Remember to specify the Grid Name/Id
    var selectedItem = grid.dataItem($(this).closest("tr"));
    alert(selectedItem.productID);


    var url = "@Url.Action("SellItem", "Home")";
    $.ajax({
        url: url,
        type: 'POST',
        data: { customerID: 1, productID: prID },
    });
 }
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Angular JS to filter ng-repeat with a combination of a field and a dropdown

There seems to be a lot of conflicting information online regarding this issue, but I am in search of a concise solution. My dataset consists of a list of countries around the world, including their name, ISO alpha code, region, and more. To display this ...

Using AngularJS to dynamically swap out {{post.title}} with a different HTML file

I want to update the value of {{post.title}} within my HTML to redirect to another HTML file. <div ng-repeat="post in posts"> <h2> {{post.title}} <a ng-click="editPost(post._id)" class="pull-r ...

Exploring the world of WebSockets and Socket.io

Recently many online games, like , have started using WebSockets to create real-time MMORPGs. I'm curious about how to develop a node.js program that can manage connections from browsers using WebSockets. Here is an example of browser code: <!DOC ...

Converting a JavaScript function to asynchronous with callback functionality

In the past, I had implemented certain methods in MyClass: MyClass.prototype.method1 = function(data1) { return this.data111.push(data1); }; MyClass.prototype.method2 = function(i) { var data = this.method1(i); if (data.condition1 != null ...

Tallying outcomes using JavaScript

I encountered a particular challenge: I have designed a table for user interaction, with results displayed at the end of each row. Just out of curiosity, I would like to count how many results are present in the table without performing any calculations. I ...

A class with Three.js

I attempted to consolidate all the necessary functionalities into a single class to create a straightforward three.js scene with a cube. Despite not encountering any errors, the scene remains black when viewed in the browser. Here is the code I've wri ...

Creating an interactive animation of bouncing balls within an HTML5 canvas using JavaScript

function refBalls(){ var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); var circles = [{x:40,y:100,r:20,color:'black',vx:5,vy:10}] function draw(){ ctx.beginPath(); ctx.arc(circles[0].x, circles[0].y, circles[0].r, ...

Significant lag experienced when using $rootscope.$on with a large object

In my AngularJS project, I am working with a JavaScript object (factory) that contains numerous functions spanning 4000 lines. Creating the object from data fetched from PHP happens pretty quickly. $http.get('pivots/list.php') .succe ...

The following working day with Moment.js

I'm having an issue trying to retrieve the next business day with my code. Currently, it is only displaying the day immediately following. Despite looking into similar questions for a solution, I have yet to identify the error. While this question is ...

When trying to show a Vue view, there was an issue with reading properties of null, specifically the 'style' property

I am experiencing an issue with a header that has an @click event in the body. Instead of displaying a Vue view upon clicking, I am encountering the following error: Cannot read properties of null (reading 'style') Upon researching on Stack Ove ...

How can I use vanilla JavaScript to retrieve all elements within the body tag while excluding a specific div and its descendants?

My goal is to identify all elements within the body tag, except for one specific element with a class of "hidden" and its children. Here is the variable that stores all elements in the body: allTagsInBody = document.body.getElementsByTagName('*&apos ...

Changing a JSON object into a List in C#

I am dealing with a Json string that looks like this: {"1":"","2":"","3":"","4":"","5":"1","6":"","7":"","8":"1","9":"","10":"1","11":"","12":"","13":"1"} The goal is to convert this into an array structure similar to this: 0: Id=1, Value="" 1: Id=2, Va ...

Obtain decrypted information from the token

I am facing difficulty in retrieving decrypted user data for the current user. Every time a user logs in, they receive a token. After logging in, I can take a photo and send it to the server. Looking at my code, you can see that this request requires a to ...

Retrieve, establish cookies, and guard against CSRF attacks

Having some difficulty with CSRF in my application while using Isomorphic fetch. The backend sends a CSRF-TOKEN in the set-cookies property: https://i.sstatic.net/duODj.png There is advice against directly accessing these cookies in code, so I attempted ...

The method .ExecuteCommand() in LINQ-to-SQL does not function properly when using parameterized object names

I'm a bit puzzled by this situation and am seeking clarification. I want to programmatically disable a foreign key constraint using my LINQ-to-SQL data context. It seems like it should be straightforward with the following code: context.ExecuteComma ...

Using JavaScript to retrieve comma-separated values depending on a specific condition

Hey there, I am encountering a problem with filtering out values from an array of objects. Essentially, I have an array of objects const arr = [ { "id": null, "name": null, "role": "Authorized ...

Discover the versions of key libraries/modules within the webpack bundle

Is it possible to identify all the libraries, scripts, or modules included in a webpack bundle through the developer's console of a website that is already running, without access to its codebase? Additionally, is there a way to determine the version ...

Could someone please assist me in figuring out the issue with my current three.js code that is preventing it from

Recently, I decided to delve into learning three.js and followed the getting started code on the official documentation. However, I encountered a frustrating issue where the scene refused to render, leaving me completely perplexed. Here is my index.html: ...

Encountered a CastError in Mongoose when trying to cast the value "Object" to a string

I am struggling with a Mongoose CastError issue within my Node.js API. The problem arises at a specific route where data is being returned appended with some additional information. Despite finding various solutions for similar problems, my scenario seems ...

Tips for resizing a tooltip using CSS

I am currently working on customizing tooltips and would like them to display right below the hover text in a responsive manner, with the ability to have multiline text. Instead of spreading horizontally, I want the tooltip to expand vertically based on th ...