Tips for sending multiple post parameters to a web API in Angular using TypeScript

I am looking to send multiple values to a web API using AngularJS TypeScript.

// POST api/values
public void Post([FromBody]string value1, [FromBody]string value2)
{
}

I want to make the method call like this:

$http.post('api/values', ???)

I need to validate data on the page by passing multiple parameters to the database. I tried using GET instead of post but it didn't work for me.

Any insights or suggestions would be appreciated.

Thanks, Hari C

Answer №1

If you want to read multiple values "FromBody", it is recommended to define a "Request" class with all the necessary parameters:

    public class Request
    {
        public string Value1 { get; set; } 
        public string Value2 { get; set; } 
    }

    //POST api/values
    public void Post([FromBody]Request request)
    {
    }

Following the advice like Aran suggested, you can make the call in this manner:

$http.post('api/values', {Value1:"foo", Value2:"bar"});

Answer №2

To send your parameters, utilize the data property which serves as the second argument of the $http.post function:

$http.post('api/data', {a:"apple", b:"banana"});

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

What are the best methods for detecting an invalid URL?

Currently, I'm constructing a basic HTML website using Angular JS ui-router. To handle scenarios where users input invalid URLs like 'www.mysite/#/invalidUrl', I've set up a custom 404 page ('www.mysite/#/error/404') with the ...

Challenge with sharing an array from a different component in Angular using @Input()

Recently, I started learning Angular and decided to create a basic blog application. While trying to retrieve a property from another component using the @Input() decorator, I encountered an issue specifically related to arrays. In a post-list-item compone ...

Check to see whether the coordinates fall inside the specified bounding box

I am faced with the task of creating a function that can determine whether a given coordinate c lies within the boundaries of coordinates a and b. All variables in this scenario are of type: type Coordinate = { lat: number; lon: number; }; Initially ...

Is there a way to give a model of a directive scope a dynamic name while also enabling 2-way binding?

I have a challenge where I need to dynamically pass object values into a directive using ng-repeat. Here's what I've attempted so far: <td ng-repeat="attr in demographicAttrs"> <yes-no name="{{ attr }}-{{ $index }}" model="ambassa ...

Implementing serialization and deserialization functionality in Typescript for classes containing nested maps

I am currently facing a challenge in transforming Typescript code into NodeJS, specifically dealing with classes that contain Map fields of objects. I have been experimenting with the class-transformer package for serialization and deserialization (to JSON ...

Is it possible to initialize the ng-model's value using other values within the scope?

I am attempting to calculate the product of two values entered by the user and then assign that result as the ng-model for a third value. However, I am having trouble getting this code to function properly. <input type="number" ng-model="a"> <inp ...

Typescript challenge: Implementing a route render attribute in React with TypeScript

My component has props named project which are passed through a Link to a Route. Here's how it looks (the project object goes in the state extended property): <Link to={{ pathname: path, state: { project, }, }} key={project. ...

How to transfer data between components in Angular 6 using a service

I'm facing an issue with passing data between the course-detail component and the course-play component. I tried using a shared service and BehaviorSubject, but it didn't work as expected. Strangely, there are no errors thrown, and the data remai ...

Using TypeScript, implement a function that is called when a React checkbox's state changes to true

Currently, I am experimenting with the react feature called onChange. My goal is to update local data by adding a value when a checkbox is selected. Conversely, when the checkbox is unselected, I just want to display the original data. However, I find that ...

After deploying on Vercel, Next.js' getServerSideProps function is returning undefined

I am trying to create a Netflix-inspired website using next.js. I am able to fetch movie data from TMDB using getServerSideProps(). While everything works as expected in development mode, once deployed on Vercel (re-deployed multiple times), the props I re ...

Utilizing a Bootstrap 3 modal within an AngularJS application with html5mode

How to display Bootstrap Modal in the first click when using html5Mode(true). When clicking on launch demo modal, the URL changes to /#myModal upon the 1st click but the modal does not appear. The modal only appears upon clicking launch demo modal again. ...

Is it possible for me to employ T extends Contravariant<any> to effectively restrict a generic type to a union of Contravariants

Let's begin by clarifying some terms: type Contravariant<T> = (t: T) => void; declare let cNum: Contravariant<number>; declare let cStr: Contravariant<string>; Now, suppose I wish to create a function that accepts an array of the ...

Interactive loadChild components

I've been attempting to dynamically import routes from a configuration file using the following code snippet: export function buildRoutes(options: any, router: Router, roles: string[]): Routes { const lazyRoutes: Routes = Object.keys(options) ...

JavaScript Function Not Executed in Bottom Section

I've implemented AngularJS includes in my project. Below is the code snippet from my index.html: <!DOCTYPE html> <html ng-app=""> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> ...

After refreshing the page, the ngRoute white page is displayed

I've encountered an issue with my Angular website. I built it using ngRoute, but when I click on a link, a white page appears. Only after refreshing the page does the content actually show up. Even in the browser's DevTools, you can see the html ...

Exploring the power of Prosemirror with NextJS through Tiptap v2

Greetings everyone, I am a newcomer to Stack Overflow and I am reaching out for assistance regarding an issue that has arisen. The problem at hand pertains to the development of the Minimum Viable Product (MVP) for my startup which specializes in creating ...

"An ng-repeat directive with a filter applied results in an empty

After successfully implementing the ng-repeat loop below: <div ng-repeat="einschItem in einschaetzungen.alldata | filter: { savedatum: lolatage[tagarrayindex].tagestring } | orderBy : '-savetimestamp'"> I wanted to check if the filtered r ...

Ways to access information received from AngularJS in a different Javascript file

I am currently using Angular to retrieve output from a controller and display it using ng-bind. However, I have another separate JavaScript file that needs to utilize a value returned from Angular. In the example code provided below, the TestAppCtrl is ca ...

Angular ng-dirty state doesn't change when using a datepicker directive

I wrote a custom directive that utilizes jQuery UI Datepicker to apply it to specific inputs. Everything works as expected - I can select a date and it updates the input. However, when I try to post back the results, the input (control) that was edited doe ...

Encountering a CORS header issue while working with the Authorization header

Here is the code snippet I am currently working with: https://i.stack.imgur.com/DYnny.png Removing the Authorization header from the headers results in a successful request and response. However, including the Authorization header leads to an error. http ...