Execute the controller function with the value as a parameter

I encountered an issue while attempting to call a function in the c# controller and passing a value. The error message I received was: `'Unable to get property 'then' of undefined or null reference'. I also included the Driver Model but still faced this problem. Thank you in advance for any assistance.

JS

function getDrivers(lastName) 
{
    console.log(lastName);
    return $http.post(baseUrl + 'Admin/getDrivers', { lastName: lastName })
    .success(function (data, status, headers, config) {
        console.log(1);
    }).error(function (data, status, header, config) {
        console.log(2);
    });
}

C# Controller

 [HttpPost]
        public JsonResult getDrivers(DriverModel model)
        {

            JsonResult jsonResult = new JsonResult();
            try
            {
                jsonResult.Data = model.GetDrivers(model.lastName);
                return jsonResult;
            }
            catch (Exception ex)
            {
                return jsonResult;
            }
        }

HTML

vm.getMatches = function (searchText) {
if (searchText.length > 1)
{
    getDrivers(searchText);
}

Answer №1

It seems like the issue may be that the getDrivers function is looking for a parameter named "lastname" but your controller is using the parameter "model" instead, which should be a string as well. Make sure the parameter names match in both the function and the controller.

Answer №2

It seems that your $http promise callback is not properly returning a value, which could result in it returning undefined instead of the desired promise.

Furthermore, please note that success and error are no longer recommended and have been replaced by then and catch. This is something worth taking into consideration for your code.

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

Share JSON data across functions by calling a function

I am currently working on a project where I need to load JSON using a JavaScript function and then make the loaded JSON objects accessible to other functions in the same namespace. However, I have encountered some difficulties in achieving this. Even after ...

Unable to load the manually added module in the /node_modules/ folder

I'm trying to manually use a module that I placed in the /node_modules/ directory. After copying and pasting the files and installing dependencies with npm, I encountered an issue while using NWJS 0.16.0. When attempting var speech = require('sp ...

Tips for optimizing the page speed of your Pixi JS app by ensuring it runs efficiently after the page loads

On my website, I implemented a dynamic gradient animation with shape-changing blobs using pixi js. The animation functions flawlessly, but the issue arises when I run page speed tests - the test assumes that the page has not finished rendering because the ...

The requested cast is invalid when converting SQL Server data to an array

Database: When attempting to transform SQL Server data into an array, I encountered an issue: Invalid specific cast error This is the code I used: string query = "select part1,part2,part3 from Table_3 where id ='" + textBox1.Text + "'"; int ...

AngularJS Gallery Showcase

Trying to showcase a collection of videos in a modal using angular.js, I decided to implement the solution from . However, I encountered difficulty in integrating my scope variables into the "html5gallery" class. <div class="html5gallery" data-skin="ho ...

Updating a hidden checkbox in an HTML input: A step-by-step guide

Currently, I have an HTML input page set up to input scores into a grid. status valueA valueB checkboxA checkboxB 1 4 5 2 x x 2 3 3 1 x 3 5 7 2 4 ...

Creating a simple Node.js project using material web components - a step-by-step guide

I have recently started experimenting with Node.js. This is a basic program I created to test the integration of material-components-web. I followed the instructions provided in the readme file and here's what I have implemented so far: package.json ...

How should a successful post request be properly redirected in React?

I am in the process of learning React and currently working on a small project. I have set up a NodeJS server to handle my requests, and now I am facing an issue with redirecting the user after a successful login. I successfully dispatch an action and upda ...

Deducting time from the present moment

I am facing a scenario where I have 2 strings representing the minute and hour for a specific function to execute at. I aim to validate if the specified minute and hour, present in string format and retrieved from my database, are within a 5-minute window ...

Ways to conceal a badge using JavaScript

Can anyone help me figure out how to edit the data-count of a badge and hide the badge using JavaScript in a Bootstrap 5 navbar with a stacked Font Awesome icon? I have tried the following: document.getElementsByClassName("p1 fa-stack fa-2x has-badge ...

Display a collection of Mongoose objects in a React component

In my development stack, I rely on node js + React. The data I work with comes from mongoose and typically follows this format: { "_id": "61b711721ad6657fd07ed8ae", "url": "/esports/match/natus-vincere-vs-team-liquid- ...

I keep encountering an issue with npm where it is unable to identify the executable to run whenever I launch VS Code and attempt to use Cypress

Whenever I open Visual Studio Code and try to run 'npx open cypress', I keep encountering this message in my terminal: npm ERR! could not determine executable to run Oddly enough, running 'npx cypress -v' provides version details, so ...

"Typescript: Unraveling the Depths of Nested

Having trouble looping through nested arrays in a function that returns a statement. selectInputFilter(enteredText, filter) { if (this.searchType === 3) { return (enteredText['actors'][0]['surname'].toLocaleLowerCase().ind ...

Gulp: executing a task with no specified output location

I am attempting to create a straightforward task to display the file size for each file in an array of paths using the gulp-size plugin, as shown below: var gulp = require('gulp') var size = require('gulp-size') gulp.task('size&a ...

Is there a built-in method called router.reload in vue-router?

Upon reviewing this pull request: The addition of a router.reload() method is proposed. This would enable reloading with the current path and triggering the data hook again. However, when attempting to execute the command below from a Vue component: th ...

Optimal strategy for waiting until all service calls have completed and returned responses within an Angular controller

Is there a way in Angular to determine when all service calls have completed so that I can stop a loading bar in the controller? ...

The issue of a false value not being correctly matched in Jasmine is causing problems

Currently, I am utilizing the following code to evaluate an element with aria-checked="false". expect((accessPolicyPage.listSelectAll).getAttribute("aria-checked")).toEqual("false"); The output is presenting as Expected [ 'false' ] to equal &ap ...

Trying to access different streams other than the standard ones may lead to encountering a

My main objective is to extract zone information from a batch file downloaded from the internet. I have based my implementation on various resources, including this SO answer and the MSDN documentation of UnmanagedMemoryStream. Below is the code I have com ...

Controller unable to properly increment values

$scope.isChecked = function(id){ var i=0,j=0,k=0; //$scope.abc[i].usertype[j].keywords[0].key_bool=true; if($scope.abc[i].type_selected == true){ while($scope.abc[i].usertype.length){ while($scope.abc[i].userty ...

Executing PHP scripts using Ajax

Check out the code snippet below: <?php //echo $this->Html->css(array('bootstrap', 'mark', 'style')); echo $this->Html->script(array('timer','swfobject','bootstrap.min.js')); // ...