Click here to Skip to main content
14,735,523 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1- this is my model
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
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();

3-this html page
<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>

4-this is anular js

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 () {$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.

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
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
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 };
url: '/API/TaxesSliced',
type: 'POST',
cache: false,
contentType: "application/json; charset=utf-8",
error: function (msg) {
showAlert('Error', msg, 5000);
success: function (data) {
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.
[no name] 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