Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1- this is my model
C#
public class TaxSliced
 {
     [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
     public int ID { get; set; }
     public string Name { get; set; }
     public int From { get; set; }
     public int To { get; set; }
 }


2- this is web api
C#
public class TaxesSlicedController : ApiController
{
    TaxesSlicedBLL taxesSlicedBLL;
    // GET api/taxsliced
    public IEnumerable<TaxSliced> Get()
    {
        using (taxesSlicedBLL = new TaxesSlicedBLL())
        {
            return taxesSlicedBLL.GetAll(null);
        }
    }
    // POST api/taxsliced
    public void Post(TaxSliced entity)
    {
        if (ModelState.IsValid && entity != null)
        {
            taxesSlicedBLL = new TaxesSlicedBLL();
            taxesSlicedBLL.Add(entity);
        }
    }
}


3-this html page
HTML
<form name="form" role="form" novalidate>
    <label>{{translation.NameAR}}</label><br />
    <input type="text"  ng-model="TaxSliced.NameAR">
    <br />
    <label>{{translation.NameEN}}</label><br />
    <input type="text"  ng-model="TaxSliced.NameEN">
    <br />
    <label>{{translation.From}}</label><br />
    <input type="number"  ng-model="TaxSliced.From" required>
    <br />
    <label>{{translation.To}}</label><br />
    <input type="number"  ng-model="TaxSliced.To" min="0" max="9999" required>
    <br />
    <button type="submit" ng-click="addTax()" >{{translation.Add}}</button>
    <button ng-click="cancelAddTax()">{{translation.Cancel}}</button>
</form>


4-this is anular js

JavaScript
app.factory('taxesSliceRepository', ['$resource', function ($resource) {
    return angular.fromJson($resource('/TaxesSliced/:id', { id: '@id' }, {
        update: { method: 'PUT' },
        save: { method: 'POST'}
    }));
}]);

app.controller('TaxesSlicedController', ['$scope', 'taxesSliceRepository', function ($scope, taxesSliceRepository) {
    $scope.addTax = function () {
        taxesSliceRepository.save($scope.TaxSliced);
    }
}]);


when i trying add data before go to server shows this error
becouse from and to are int data type when i changed data type to string it's works

POST http://localhost:10168/TaxesSliced 500 (Internal Server Error)

Message":"An error has occurred.","ExceptionMessage":"Property 'From' on type 'TaxSliced' is invalid.
Posted

1 solution

have you injected your app with ngRoute?

if not have a look at this example $resource[^] there is a complete example at the bottom of the page.

Edit: Added this to explain comments from user
JavaScript
var app = angular.module('AppName',[]);
app.controller("ControllerName", function($scope, $http) {
  $scope.CallAPI = function() {
  var request = $http({
                 method: "post",
                 url: '/WebApiControllerName/TaxesSLiced',
                 data: { entity: TaxSliced }
  });

  request.success(function(data) {
    //do what is needed here
  });

  request.error(function(error, status) {
    //do what is needed here
  });
 };
});
 
Share this answer
 
v3
Comments
Golden Mind 3-Nov-14 7:22am    
var app = angular.module('app', ['ngRoute', 'ngResource', 'ngCookies']);

this probelm with web api

TaxSliced = { TaxSlicedID: 0, NameEN: 'aaa', NameAR: 'sss', From: 10, To: 110, Tax: 10 };
$.ajax({
url: '/API/TaxesSliced',
type: 'POST',
cache: false,
contentType: "application/json; charset=utf-8",
data:JSON.stringify(TaxSliced),
error: function (msg) {
showAlert('Error', msg, 5000);
},
success: function (data) {
alert(data.NameEN);
}
});
this code not work
but when i change url from web api to MVC Controller it's woek successfuly
Simon_Whale 3-Nov-14 7:36am    
As far as I am aware all the Web API is, is a set of controllers with out the views attached which is why I use the controller name when making api calls. I have attached an example of how I would of called the controller using angular methods.
RaisKazi 3-Nov-14 16:07pm    
My 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900