AngularJS not successfully invoking web API method

Recently, I started using angularjs to develop a basic web application. Below is the code structure of my simple view displaying a list of candidates and allowing me to search for candidates in the database.

Here's my Javascript controller:

appRoot.controller('CandidateController', function ($scope, $location, $resource) {
    var searchcandidates={

        Name:$("#txtname").val(),
        Email:$("#txtemail").val()
    };
    var Listcandidates = $resource('/api/Candidate', {Name:$("#txtname").val(), Email:$("#txtemail").val()}, { update: { method: 'GET' } });

    $scope.candidateslist = [];

     Listcandidates.query(function (data) {
        $scope.candidateslist.length = 0;
        angular.forEach(data, function (CandidateData) {
            $scope.candidateslist.push(CandidateData);
        })
    });

     $scope.SearchCandidates = function () {
         alert($("#txtname").val());
         var list = $resource('/api/Candidate', { id: "2" } );

     }




    var init = function () {

    }

    init();
});

My Cshtml:

@{
    Layout = null;
}

<div class="FormHeader"><span class="searCandidate"></span>Search Result</div>
<div class="blank"></div>
<div class="container-fluid">
    <div class="row-fluid">
        <div class="span2">
            <!--Sidebar content-->

                Search:
          <label>Name</label>
                <input type="text" id="txtname" name="name" />
                <label>Email</label>
                <input type="text" id="txtemail" name="email" />
                <input type="button" value="Save" ng-click="SearchCandidates()" />

        </div>
        <div class="span10">
            <!--Body content-->

            <ul class="candidates">
                <li ng-repeat="candidate in candidateslist"
                    class="thumbnail phone-listing">
                    @*<a href="#/phones/{{phone.id}}" class="thumb"><img ng-src="{{phone.imageUrl}}"></a>
                      <a href="#/phones/{{phone.id}}">{{phone.name}}</a>*@
                    <div class="item">
                        <span class="green leftMrg">{{candidate.name}}</span>
                        <span class="leftMrg">{{candidate.skill}}</span>
                        <span class="grey leftMrg">Resume ID: {{candidate.id}}</span>
                        <div class="blank"></div>
                        <div class="blank"></div>
                    </div>
                    <div class="item">
                        <span class="bold">{{candidate.exporganization}} </span>
                        <span>Education:{{candidate.eduname}}</span>
                        <span>{{candidate.totalexperience}}</span>
                        <span>Preferred Location: {{candidate.location}}</span>
                    </div>
                    <p></p>
                </li>
            </ul>
        </div>
    </div>
</div>

My app.js:

var appRoot = angular.module('main', ['ngRoute', 'ngGrid', 'ngResource', 'angularStart.directives', 'kendo.directives']);     //Define the main module

appRoot
    .config(['$routeProvider', function ($routeProvider) {
        //Setup routes to load partial templates from server. TemplateUrl is the location for the server view (Razor .cshtml view)
        $routeProvider
            .when('/home', { templateUrl: '/home/main', controller: 'MainController' })
            .when('/contact', { templateUrl: '/home/contact', controller: 'ContactController' })
            .when('/about', { templateUrl: '/home/about', controller: 'AboutController' })
            .when('/demo', { templateUrl: '/home/demo', controller: 'DemoController' })
            .when('/candidate', { templateUrl: '/home/candidate', controller: 'CandidateController' })
            .otherwise({ redirectTo: '/home' });
    }])
    .controller('RootController', ['$scope', '$route', '$routeParams', '$location', function ($scope, $route, $routeParams, $location) {
        $scope.$on('$routeChangeSuccess', function (e, current, previous) {
            $scope.activeViewPath = $location.path();
        });
    }]);

My CandidateController.cs

public class CandidateController : ApiController
    {
        CandidateSearchDAL objCandidateSearchDAL = new CandidateSearchDAL();



        // GET api/<controller>
        public IEnumerable<CandidateModel> Get()
        {
            List<CandidateModel> lstCandidates = objCandidateSearchDAL.GetSearchCandidates();
            return lstCandidates;
        }

        // GET api/<controller>/5
        public string Get(string id)
        {
            return "value";
        }

        // POST api/<controller>
        public void Post(string Name, string Email)
        {
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }

After loading the page for the first time, it displays all the candidates perfectly by calling the controller's IEnumerable Get() method. However, when I try clicking the save button, it fails to call the controller API.

Answer №1

I don't see a phone call:

$http.get()

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

Having issues with importing momentjs by reference in TypeScript with amd configuration

I'm puzzled by the difference in behavior between these two snippets: import * as moment from "../Typings/moment"; One works, while this one doesn't: /// <reference path="../Typings/moment.d.ts" /> import * as moment from "moment"; It t ...

Ensuring a boolean outcome from a promise function within AngularJS

When working with angularjs, I am attempting to determine whether the value isInternal is true or false based on a promise function. However, instead of getting the expected result, I am receiving another promise (Promise {$$state: Object} $$state : Object ...

Bringing in the bootstrap JavaScript file

I've recently started using Bootstrap that I installed from npm, but I'm having trouble importing the JS file. This is my main JS file where I want to utilize all the tools: import * as bootstrap from '../node_modules/bootstrap/dist/js/boot ...

Click on a button to send the React Router path as a parameter in

I've got a React form with a submission button like this: <Link className="btn btn-secondary btn-width-200 search-submit" to={{pathname: '/booking/search', query: this.state.filters}}> Search </Link> Within the ...

The proxy is unable to access the method within the subclass

Issues are arising as I attempt to utilize a Proxy. The structure of my class is as follows: export class Builder { public doSomething(...args: (string | number | Raw | Object)[]): this { // Do stuff return this } } export class M ...

The functionality of passing an ID in Bootstrap Modal is not functioning as expected

I'm facing an issue with passing an ID to my modal. My goal is to have the details of an event displayed in the modal when the corresponding link (which represents the event) is clicked. However, I'm struggling to retrieve the ID and show the ev ...

Verifying API functionality with a mocha test for the PUT endpoint

My mocha test is producing an error message: "Uncaught AssertionError: undefined == 'Ernest'. It seems like the test may be referring to the song instance created at the beginning of the code. I'm unsure about how to resolve this issue. Thi ...

How to Transfer a Props Value to an External Function in Vue 3

I'm currently working on a page that displays user details and need to also show the invoices associated with that user. <template> <div> <div> // Child component one ..... </div> <div ...

Loading of iframes occurs only after receiving the content sent through the postMessage function

I have a scenario where an iframe is used to receive information through the contentWindow.postMessage function in order to log in to a page that I am creating. However, I am facing an issue where the page loads before the contentWindow.postMessage message ...

What is the best way to enable this JavaScript function exclusively for a particular width?

Is there a way to make this javascript function active only when the screen width is at least 1070px? Currently, I have two different headers for screens smaller than 1070px, causing padding to be added regardless of the presence of "navbar_top". Can med ...

Issue navigating with Angular routes

I've been struggling with routing for the past couple of days without any success. I've gone through various questions on stackoverflow and tried implementing the solutions provided in the answers, but to no avail. I included the route script in ...

Tips for including a decimal point in an angular reactive form control when the initial value is 1 or higher

I am trying to input a decimal number with 1 and one zero like 1.0 <input type="number" formControlName="global_velocity_weight" /> this.form = this.fb.group({ global_velocity_weight: new FormControl(1.0, { validators: [Valida ...

How can I display several custom markers that are constantly updating on a Google map with MySQL and PHP?

Currently, I am using the following code to generate markers on a Google map by retrieving data from a database. However, the issue I am facing is that it only generates one marker instead of all the markers stored in the database. & ...

Tips on showcasing information from a deeply nested JSON structure with jQuery

Today I am putting on my front-end hat and tackling a small challenge. I have created an API that generates a directory tree and provides me with a JSON structure like this: { "0": { "children": [ { "name": "still.t ...

Tips for iterating within a Vue.js template without disrupting the HTML structure

Currently, I am attempting to implement a loop within a table. However, I have encountered an issue with the following code: <tr v-for="( block , index ) in listRenderBlock" :key="index"> <div v-for="( section , i ) in ...

Arrange the array in a peculiar manner

If I have an array structured as follows: let array = ["eid_x", "eid_x", "cid_x", "cid_x"] What would be the best way to sort it in the following order? let array = ["cid_x", "eid_x", "cid_x", "eid_x"] The initial array is arranged randomly, for exampl ...

Determining in Express.js whether headers have been sent or not

As I develop a library that involves setting headers, I aim to provide a personalized error message in case the headers have already been sent. Rather than simply letting it fail with the default "Can't set headers after they are sent" message from No ...

Ajax removes the selection from the hyperlink under the mouse cursor

Currently, my webpage is set up to constantly refresh a div using an Ajax request every second. Within this div are hyperlinks. Unfortunately, when users hover over one of the hyperlinks and pause for a moment without moving their cursor, the "hover" pseu ...

Is it advisable to place custom middleware at the end of the chain in a customized next.js server setup?

I am facing a challenge in integrating a custom middleware into a bespoke next.js server. The issue lies in the fact that there are no set path patterns to which I can bind my middleware, as our CMS generates URLs of varied forms and structures. Therefore, ...

Infinite scrolling comes to a halt and ceases to load any more results after reaching the

My index page has an endless scrolling feature, with a database containing over two hundred users. The page is set up to display four users per page, allowing me to scroll down to a maximum of 6 pages. However, I am facing an issue where it doesn't lo ...