0

I have a web api developed. I need to pass an array of objects

[{"Id":"10010","lati":"12.991845763535506","longi":"77.54596710205078","PSID":"1001"},
 {"Id":"10011","lati":"12.97846402705198","longi":"77.55729675292969","PSID":"1001"},
 {"Id":"10012","lati":"12.967758119178907","longi":"77.54425048828125","PSID":"1001"}]

The model class of web api is given below

Locate.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace webapi.Models
{
    public class Locate
    {
       [Key][Required]
       public string Id { get; set; }
       public string lati { get; set; }
       public string longi { get; set; }
       public string PSID { get; set; }
     }
 }

and the code corresponding to post method in controller file is given below

LocatesController.cs

  // POST: api/Locates
    [ResponseType(typeof(Locate))]
    public async Task<IHttpActionResult> PostLocate(Locate locate)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.Locates.Add(locate);

        try
        {
            await db.SaveChangesAsync();
        }
        catch (DbUpdateException)
        {
          if (LocateExists(locate.Id))
            {
                return Conflict();
            }
            else
            {
                throw;
            }
        }

        return CreatedAtRoute("DefaultApi", new { id = locate.Id }, locate);
    }

    private bool LocateExists(string id)
    {
        return db.Locates.Count(e => e.Id == id) > 0;
    }

I send the http post request in my js script given below

app.js

  $scope.adding = function()
  {    
    var idd = $rootScope.vaar;
    var datas = [];
    var len = latitudes.length;
    for (var i = 0; i < len; i++) {
        datas.push({
            "Id": idd + i.toString(),
            "lati": latitudes[i].toString(),
            "longi": longitudes[i].toString(),
            "PSID": idd
        });
    }
 var jsonData = angular.Json(datas);
  var objectToSerialize = {'object':jsonData};
  var data = $.param(objectToSerialize);
   var config = {
        headers: {
            'Content-Type': 'application/-www-form-urlencoded;charset=utf-8'
        }
    }

    $http.post('http://localhost:8080/pool/api/locates/', data,config).success(function (data, status, headers, config) {
        alert("Success");
            }).error(function (data, status, header, config) {
            alert("An error has occured while adding!"+status);
             });
        }

It does not add the above array. Please help me

5
  • Are you getting the correct Json in data Commented Jul 27, 2016 at 11:43
  • Change angular.Json(datas) to angular.toJson(datas) Commented Jul 27, 2016 at 11:44
  • Your content type should be application/json. And your controller is expecting an object but you are passing an array. Commented Jul 27, 2016 at 11:48
  • My latitudes array is not empty. I used angular.toJson(datas). Its not working. The postman also throws error status 400 Commented Jul 27, 2016 at 11:50
  • you doesnt need to do angular.Json()(It seems angular doesnt have this function) Commented Jul 27, 2016 at 11:54

2 Answers 2

1

the problem is here:

[ResponseType(typeof(Locate))]
public async Task<IHttpActionResult> PostLocate(Locate locate)

and you are posting an array so it should be:

[ResponseType(typeof(Locate))]
public async Task<IHttpActionResult> PostLocate(List<Locate> locates)
Sign up to request clarification or add additional context in comments.

1 Comment

The controller is expecting a single item so doing this will still be problematic. It is unclear if OP wants one Locate or if maybe Locate has a property that is a list of coordinates and doesn't know how to set it.
0

The problem is solved. I changed the parameter to (List<Locate> locates) and made a logic inside post method. Thank you Glenn Packer

Comments

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.