Angularjs $http.post but asp.net 5 controller gets null

angularjs post to mvc controller c#
$http.post angularjs
call web api post method from mvc controller c#
http get angularjs mvc
angularjs $http post array parameter
angularjs $http post form data
call web api from mvc controller with parameters
how to send data from angularjs to servlet

In my angularjs code I have this:

$http.post("Home/PostData", {name:'Peter',age:18}).success(function (response) {

});

And I have this method in my asp.net 5 controller:

[HttpPost]
public IActionResult PostData(string name,int age)
{

}

I've put a break point on the method and it gets fired, but my name is null and age is 0.

I have done that before by using asp.net mvc 5 and that worked.

Why is wrong in asp.net 5?

Change your controller code like below:

public class PostModel
{
    public string name { get; set; }
    public int age { get; set; }
}
[HttpPost]
public IActionResult PostData([FromBody]PostModel model)
{

}

The Controller action method will be called using AngularJS $http service The value of the name parameter is assigned to the Name property of the to Controller using AngularJS $http POST service in ASP.Net MVC 5. headers - List of headers to be specified for the HTTP Request. Layout = null;. AngularJS CRUD Operation in ASP.NET MVC. Here, you will learn how to perform CRUD operation (Create, Read, Update and Delete) using AngularJS with ASP.NET MVC 5 with Visual Studio 2013.

Use,

$http.post("Home/PostData", { params: {name:'Peter',age:18}}).success(function (response) {

});

I am implementing an $http.get() in AngularJS that passes an object to what you are doing, it might be better to send it using an HTTP POST. The Controller action method will be called using AngularJS $http service with AJAX and JSON from View in ASP.Net MVC 5 Razor.

Try this

 $http.post("Home/PostData",{ name: "Peter", age:18}).success(function (response){

    });

To force Web API to read a simple type from the request body, add the [FromBody​] attribute to the parameter: Hide Copy Code. [HttpPost]  ASP.NET (286) jQuery (221) ASP.NET MVC (86) JavaScript (85) LINQ (83) C# (79) Silverlight (76) Free Learning (63) Product Releases (61) VB.NET (60) Visual Studio (60).NET (49) Misc Ramblings (46) Tools (46) CSS (28) Windows 7 (22) WPF (20) Free EBook (19) HTML 5 (16) ASP.NET Web API (15) Sharepoint (15) Azure (14) ASP.NET AJAX (13) Link List

In my case controller parameter name is same name as property in the model. Its failing because of that reason.

For Ex- in controller public JsonResult Method(BindingModel firstname, ViewState viewState)

in BindingModel--

class BindingModel{ string firstname, string lastname }

Angular2 Http post method calls the controller action and works properly when there is no parameter/properties, but when i add a parameter to  Let’s kick things off by taking a look at a basic backend service written using ASP.NET Web API and then walk through several AngularJS features including a factory that talks to the service. Creating a RESTful Service. ASP.NET Web API is a great back-end framework for exposing data to a variety of clients. Although I’ll focus on how JSON

We inject the $http service and create two functions that use it: getArtists() and createArtist(). code behind work over HTTP POST by default, so we use the .​post() method are accessible in the following format: pageName.aspx/​methodName. parameters (it's essentially readonly), we can't pass null as the request body  And I have this method in my asp.net 5 controller: [HttpPost] public IActionResult PostData(string name,int age) { } I've put a break point on the method and it gets fired, but my name is null and age is 0. I have done that before by using asp.net mvc 5 and that worked. Why is wrong in asp.net 5?

ASP.NET Core's default API handlers return an HTTP 204 response for null value results. This is meant to be a convenience feature, but if your calling API ASP.​NET • C# • HTML5 • JavaScript • AngularJs February 24, 2020 • from Maui, Hawaii • 5 comments. :P The second request returns null from the controller action:. As I said earlier, we are going to use the TreeMap plugin in our MVC application with AngularJS. For this, you must have Visual Studio 2015 (.NET Framework 4.5.2) and SQL Server. SQL Database part

Angular HTTP client is the toolkit which enables us to send and receive entity and we are going to demo the Get, Post, Put and Delete Requests. Next is Adding the controller which will be a Web API controller and we can ID == id);; if (employee == null); {; return NotFound();; }; return Ok(employee);; }  The ASP.NET server side code (in this case it is Action method of the controller of ASP.NET MVC project) simply uses Request.Form["keyname"] to retrieve data sent from the AngularJS and returning it as ContentResult.

Comments
  • Thank you adem caglin, you saved my day!