Learn the process of updating database information with AngularJS through a button click

As a newcomer to Angular, I am facing an issue in my mini project. The main goal of the project is to display data from a database. I have successfully set up the view to show current data by making API calls and connecting to the database. However, I am now trying to figure out how to update the data in the database when a button is clicked. I'm unsure of what kind of function I should write in my controller for this task.

var app = angular.module('myApp', ['ngResource']);
app.controller('myCtrl', function ($scope, $resource, $http, $routeParams, $location) {

    var Users = $resource('/api/tables');
    $scope.users = Users.query(function () {
        //console.log(user);
    });
});

In my (API) Tables1Controller.cs file, here is the main method:

public string GetTables()
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Serialize(db.Tables);
        } 

And in my Index file where the data is displayed, I have added a button:

<table class="table">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Surname</th>
            <th>Address</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="user in users">
            <td>{{user.Id}}</td>
            <td>{{user.Name}}</td>
            <td>{{user.Surename}}</td>
            <td>{{user.Address}}</td>
        </tr>
    </tbody>
</table>

<div ng-repeat=>
    {{ user.Name }}
    a
</div>
<div class="btn btn-danger">Button</div>

In _Layout.cshtml, I have added ng-app and ng-controller to the body tag. I believe using ng-click near the button could be a starting point, but I'm unfamiliar with how to write the function to handle the update process.

Answer №1

It appears that you are utilizing ASP.Net MVC in conjunction with Angular. It can be tricky because the term 'controller' carries different meanings in the context of MVC and Angular.

To address your question, there are two key steps to follow:

  1. Angular: Utilize $http.post to send data when the button is clicked by the user. Make sure that the URL specified in $http.post matches the route defined in step #2.
  2. MVC controller: Create a HttpPost controller to receive data from the browser. Then execute an insert command to store the data in your storage system.

Answer №2

Issue Resolved!

I made a modification to the js file:

$scope.update = (function () {
        var Users = $resource('/api/tables');
        $scope.users = Users.query(function () {
        });
    });

Additionally, I added a button in the html file like this:

<div class="btn btn-success" ng-click="update()">Update</div>

Answer №3

For further information, check out this article on ngResource:

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

Extracting information from HTML and transferring it to Javascript using jQuery

My goal is to utilize jsGrid for showcasing database data without repeating code extensively. I aim to generate separate grids for each <div> with a specific id while passing on relevant values accordingly. To clarify my objective, here's a sni ...

Create-react-app unable to activate Service Worker

I've been utilizing the power of create-react-app to create my react.js project. Whenever I use the command npm run build, it automatically integrates a service-worker for progressive web app functionality in the production build. Everything was runn ...

What is the process for pausing a video while it is still buffering and loading?

Is it possible to suspend a video when it is in an opening or preparing state? For example, if I open a video and then switch to another application using the smart hub feature, how can I suspend the video while it is in the process of opening or preparin ...

Add fresh material to the bottom of the page using Javascript

Hey there, I'm having a bit of trouble with my page where users can post their status. I want the new posts to appear at the bottom after the older posts when the user presses the button. Currently, Ajax is placing all new posts at the top of the old ...

Encountering a fragment error while utilizing create-react-library

Recently, I embarked on the journey of publishing a React component to npm that I had created. In my quest for knowledge, I stumbled upon create-react-library, which I decided to use for the first time. As I started testing my component from the test folde ...

Setting up npm packages in Visual Studio and installing them

Currently working on an ASP.NET Core project that requires the use of jQuery UI. After successfully adding "jquery-ui": "1.12.1" to package.json, I noticed that there is no jquery-ui.min.js file in node_modules/jquery-ui directory. Instead, I found other ...

Chrome autocomplete behaves as if the input fields are disabled and cannot be clicked on

I am experiencing an unusual issue with autofill in Chrome. After logging in and then logging out of the app, the input fields (email, password) are auto-filled but appear to be frozen and unclickable. This problem does not occur every time; it only happe ...

Help with guiding and redirecting links

Here's the code snippet: <script> if(document.location.href.indexOf('https://thedomain.com/collections/all?sort_by=best-selling') > -1) { document.location.href = 'https://thedomain.com/pages/bestsellers'; } </script& ...

excess white space appears in the mobile version

I recently completed a website using both materializecss and bootstrap platforms. While I know this may not be the best practice, it worked for my needs. However, I am facing an issue with the mobile view. When I reduce the viewport size, a margin appear ...

Tips for utilizing JSON.parse

Within my ajax request, I am encountering the following code: 403: function(jqXHR) { var error = jqXHR.responseText; console.log(error); } When this error occurs, a message is displayed in the console: Htt ...

Adding an item to a JSON array in Angular

I'm having trouble adding a new item to the roleList JSON array. I've tried using push/concat, but it doesn't seem to update the roleList array. Any suggestions on how to resolve this? // The javascript : function RoleListCtrl($scope) { ...

What is the best way to eliminate the final character from a series of repeated Xs and following strings

Data consists of [one][two][three]. I want to exclude the last set of brackets and have a result like [one][two]. How can this be achieved using jQuery? ...

I am interested in incorporating the ability to select and scroll the window without needing to interact with the scroll bar by

Imagine a visitor wanting to highlight all the information on a webpage. They choose to start dragging their cursor towards the bottom of the window. How can we enable the webpage to smoothly scroll down as they do this? ...

Error status: disconnecting Zeppelin Zephyr

Currently, I am utilizing Zeppelin 0.70 and have successfully built Zeppelin from the Git source. Everything was running smoothly when I started Zeppelin on port 8000 using the command below. ./zeppelin-daemon start https://i.stack.imgur.com/tiLyF.png H ...

A guide on incorporating the section tag in AngularJS to achieve a responsive design

Is it possible to implement AngularJS on a webpage with three sections and make it responsive by converting two sections into dropdown buttons? <body> <section id="main"> <section id="body"> // content in li elements ...

Identify the Google Maps Marker currently displayed on the screen

Is there a way to generate a list of markers within the zoom range for Google Maps, similar to the functionality on this site I'm curious if I can achieve this using jQuery or if there is a built-in function for Google Maps v3? Thank you! ...

Implement the parent jQuery library within a child iframe

I have a query regarding the use of two iframes in my HTML page. I am trying to implement a single jQuery file that is loaded on the parent page. The code snippet provided below works perfectly on all browsers, except for one issue with Chrome when using t ...

Issue with parallax scroll feature on Mobile Safari not functioning as expected

I created a JavaScript code for parallax scrolling on an image at the top of a webpage. The code functions perfectly on Chrome, Firefox, Internet Explorer, Opera, and Safari on desktop. However, when I tested it on Safari on my iPad, the parallax effect wa ...

Creating a Dropdown list using Javascript

I am currently working on implementing inline CRUD operations in MVC 5. When a user clicks a specific button to add new records, it should create a dynamic table row. Below is the JavaScript code I am using: function tblnewrow() { var newrow = ' ...

When the user clicks, the template data should be displayed on the current page

I need help with rendering data from a template on the same HTML page. I want to hide the data when the back button is clicked and show it when the view button is clicked. Here is my code: <h2>Saved Deals</h2> <p>This includes deals wh ...