1

I am using angualarJS and asp.net web API but my code always breaks on post request in the asp.net controller and this is the error I got

An exception of type 'System.ArgumentNullException' occurred in EntityFramework.dll but was not handled in user code Additional information: Value cannot be null.

this is the MVC method

// POST: api/ProductCategory
public HttpResponseMessage Post(ProductCategory pcate)
{
  if (ModelState.IsValid)
  {
    this.pcat.pcategory.Add(pcate);
    this.pcat.SaveChanges();
    return Request.CreateResponse(HttpStatusCode.Created, pcate);
  }
  else
  {
    return Request.CreateResponse(HttpStatusCode.BadRequest, pcate);
  }
}

this is the angularJS controller

var appcat = angular.module("PharmacyControllers", ['ngRoute']);
    
appcat.controller("createController", ['$scope', '$http', '$location', function ($scope, $http, $location)
{
  $scope.submit = function()
  {
    //converting the object to JSON
    var obj = {
      Name: $scope.Name
    };
    //alert(obj);
    $http.post("/api/productCategory/", this.obj).success(function (data) {
       $location.path('/list');
    }).error(function (data) {
      
       $scope.error = "An error has occurred while adding product Category! " + data.ExceptionMessage;
    });
  }
    
}]);

thanks in advance

5
  • Can you show a JSON request and a input model for controller? Commented Mar 25, 2016 at 23:29
  • this is the JSON var obj = $.param({ pcate: { Name: $scope.Name } }); Commented Mar 25, 2016 at 23:40
  • 1
    Have you checked in debug mode if pcate is not null? Also have you tried to check what is actually posted? Commented Mar 25, 2016 at 23:45
  • @LLJanneh I mean API model ProductCategory and actual JSON (looks like {...}). Commented Mar 25, 2016 at 23:58
  • the API public class ProductCategory { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Key] public int CID { get; set; } [Required] public String Name { get; set; } } the JSON format is GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter()); Commented Mar 26, 2016 at 0:04

2 Answers 2

1

You are creating a local variable obj here:

var obj = {
    Name: $scope.Name
};

But then you are trying to post this.obj to your API:

$http.post("/api/productCategory/", this.obj)

Either create the this.obj instead of the var obj or pass obj instead of this.obj on your post.

Sign up to request clarification or add additional context in comments.

Comments

0

Try changing from:

$http.post("/api/productCategory/", this.obj).success(function (data) {

to:

$http.post("/api/productCategory/", { pcate: obj }).success(function (data) {

2 Comments

this is not working! I also try var obj = $.param({ pcate: { Name: $scope.Name } }); but still the same error
@LLJanneh What does the ProductCategory class look like?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.