Retrieve all objects from model in ASP.NET MVC

    $("#collectorscompleted").kendoChart({
        legend: {
            visible: false
        },
        seriesDefaults: {
            type: "column"
        },
        series: [{
            name: "Completed",
            data: [@Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID==1), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 2), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 3), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 4),
                   @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 5), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 6), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 7), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 8),
                   @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 9), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 10), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 13), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 14),
                   @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 15), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 16), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 17), @Model.CollectorStatus.Count(c => c.BatchCompleted.Value && c.CollectorID == 18)]
        }, {
            name: "Failed",
            data: [@Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID==1), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 2), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 3), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 4),
                   @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 5), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 6), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 7), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 8),
                   @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 9), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 10), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 13), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 14),
                   @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 15), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 16), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 17), @Model.CollectorStatus.Count(c => !c.BatchCompleted.Value && c.CollectorID == 18)]
        }],
        valueAxis: {
            labels: {
                visible: false
            },
            line: {
                visible: false
            },
            majorGridLines: {
                visible: false
            }
        },
        categoryAxis: {
            categories: [X], // All collectorID's from model here
            line: {
                visible: false
            },
            majorGridLines: {
                visible: false
            }
        },
        chartArea: {
            background: "none"
        },
        tooltip: {
            visible: true,
            format: "{0}",
            template: "#= series.name #: #= value #"
        }
    });

I've got the following script. However Categories are now hard typed in myself which is wrong.
My question is: How can I get all CollectorIDs from the model?

Categories: [1,2,3,4,5,6,7,8,9,10,13,14,15,16,17,18], // All collectorID's from model here

Answer №1

Utilizing the MVC architecture requires incorporating collectorIds from the model into your controller. Before initializing, ensure to call your controller using ajax. The process should resemble this:

Javascript

function GetCollectionInformationAjax()
    {
        $.ajax({
            type: "GET",
            url: "@Url.Action("GetCollectionInformation", "YourController")",
            contentType: "application/json",
            cache: false,
            success: function (data) {
                //call the initializer here
                Initialize(data);
            },
            error: function (xhr, status, error) {

            }
        });
    }

C# controller code snippet

public actionResult GetCollectionInformation()
{
    //This is just an example, make sure to modify accordingly!
    return [1,2,3,4,5,6,7,8,9,10,13,14,15,16,17,18];
}

Javascript initialization function

function Initialize(data)
{
$("#collectorscompleted").kendoChart({
    legend: {
        visible: false
    },
    seriesDefaults: {
        type: "column"
    },
    series: [{
...
]);}

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

SuperAgent - Refresh the initial request with a new bearer token upon encountering unauthorized access

Issue: I encountered a problem while attempting to resend my original request using superagent. Here is some pseudo code that I came up with: function retryRequest({ params }) { return superagent.post(url) .set("Authorization", `Bear ...

Shuffle the dots on the sphere and assign each one a unique identifier

I am currently working on creating a spherical design using three.js. My goal is to have clickable dots and meshes embedded within this sphere. To achieve this, I believe that assigning names to each dot on the sphere will be essential. I have two specific ...

Passing key value pairs with jQuery's get method

For my project, I am developing a universal function that interacts with different types of data and executes ajax get requests based on the data. At times, I need to make a get request like this: {option1: 'delete', id: idToRemove}, and other ...

What could be causing the invisibility of my drop down list items?

I am having trouble creating two cascading drop-down lists. The first one is functioning correctly; however, when I move to the second drop-down list, I can see that the correct number of options are being generated based on the items in the database, but ...

Node-red occasionally crashes and the JSON output may unexpectedly return HTML content

Having some trouble with my node-red crashing several times a day. I suspect that one of the issues may be related to an http request I am making. I'm trying to retrieve JSON data from a webpage, but sometimes I encounter errors in the log indicating ...

Generate CANNON.RigidBody using either a THREE.Mesh or THREE.Geometry object

For my project, I am using a THREE.JSONLoader to create a THREE.Mesh object as shown below: // Creating a castle. loader.load('/Meshes/CastleTower.js', function(geometry, materials) { var tmp_material = new THREE.MeshLambertMaterial(); T ...

Interactive button that dynamically alters its position and style upon being clicked, while also allowing the option to toggle between displaying or concealing text

I'm looking to add 3 buttons that can toggle between displaying and hiding text. When a button is clicked, it should shrink in size and move to a new position, allowing the text to be shown at the original location of the button. Here's an exampl ...

`Problem encountered in establishing a database table through jQuery ajax`

I am completely new to utilizing jQuery and ajax. Currently, I am experimenting with creating a table on my local MySQL server using a JavaScript file that sends SQL statements to a .php file for execution. Here is the code in the .js file: function exec ...

Iterating through a DataTable's JSON data and accessing elements by their array index

I have some JSON data structured as follows: [ {"name": "Sitepoint", "url": "http://sitepoint123.com", "editor": {"name": "John Doe", "phone": ["9191919", "1212121"], "email": []}}, {"name": "Flippa", "url": "http://flippa.com", "editor": {"na ...

Creating a versatile AngularJS function for dynamically changing button text multiple times

html : <p> <a class="btn btn-lg btn-success" href="#"ng-click="splendido()">{{salute}}</a></p> main.js $scope.splendido=function() { var calls=1; $scope.salute='I am greeting you for the first time'; ...

The information window is malfunctioning on Google Maps

I created buttons that are linked to specific locations on a map and they seem to be functioning, although not in the most efficient way. However, when attempting to add an info window to appear on the marker, it does not work as expected. I am unsure of ...

Utilizing the setNetWorkConditions function in webdriverjs for Chrome

Is there a way to properly utilize the webdriverjs setNetworkConditions() method as outlined in the official documentation? This is what my code looks like: const chromeCapabilities = webdriver.Capabilities.chrome() const chromeOptions = { ...

Implement using a variable as a key for an object in a reducer function

I am facing an issue with constructing an object. The string, named "SKU" in this scenario is being passed through action as action.name. Although I have all the necessary data in the reducer function, I need to dynamically replace the hardcoded SKU with ...

Is there a way I can utilize a for-loop and if statement in JavaScript to present the information accurately within the table?

My current task involves fetching data via AJAX and then using a for-loop and if-statement to determine which goods belong in each shopping cart. Once identified, I need to display these goods in separate tables corresponding to each customer. Although the ...

ASP.NET Web API restricts the use of extended base64 URIs

Recently, I encountered an issue while trying to handle a lengthy base64 string from my Android Client in order to decode it into a bitmap within my Web API Project for uploading as an image to Azure BLOB Storage. Unfortunately, the project kept returning ...

The function $.ajax(...) is not defined

Even when using the standard version of jQuery, I still encounter the issue. <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"& ...

Using Vuetify to send a parameter from a select option to a method as a parameter

Just starting out with vuejs and encountering an issue passing parameters from a selected option to my JavaScript method. In my updateClientIsMaster method, the item is always undefined. However, when I added v-on:change="updateClientIsMaster in the & ...

Creating dynamic buttons with event listeners and parameters in JavaScript using jQuery

I am attempting to dynamically create and append buttons to an HTML page with defined events using the following code: var mdata = JSON.parse(data); mdata.forEach(function(k) { $("#routesPage").append("<button class='accordion' onclick=&a ...

Tips on updating arrow button icon when clicked using jquery

I am currently working on a project where I have a button icon that I want to change upon clicking it. I am using the following jQuery code: <script> $('div[id^="module-tab-"]').click(function(){ $(this).next('.hi').sl ...

The onClick function within the .map function is continuously triggered

I am encountering an issue with my code where a Dialog confirmation prompt from Material UI keeps getting called unexpectedly. The problem seems to arise when I add a value to the function that is triggered by a button click within a loop of an array usi ...