0

I have an app that has the ability to get and post through http with a model of a relational database. I am able to display my data and the JSON data looks like this on http://localhost/**/api/complaint:

{
"CHECKLISTs": [{
    "COMP_ID": 1,
    "IntOIMRec": "No"
}],
"COMP_ID": 1,
"FileNum": "case1"
}

using angular:

<tr data-ng-repeat="complaint in complaints">
<td><strong data-ng-hide="complaint.editMode">{{ complaint.FileNum }}</strong></td>
<td>
<p data-ng-hide="complaint.editMode">{{ complaint.CHECKLISTs[0].IntIAB}}</p>
<input data-ng-show="complaint.editMode" type="text" data-ng-model="customer.Status" />
</tr>

I am also able to http post with this controller:

//Insert complaint
$scope.add = function () {
    $scope.loading = true;
    $http.post('/api/Complaint/', this.newcomplaint).success(function (data) {
        alert("Added Successfully!!");
        $scope.addMode = false;
        $scope.complaints.push(data);
        $scope.loading = false;
    }).error(function (data) {
        $scope.error = "An Error has occured while Adding complaint! " + data;
        $scope.loading = false;
    });
};

But when I add the Checklists[0] to my form to add it, it will not add a new record. My code for the form to add a new record looiks like this:

<div class="row">
    <div class="col-md-12">
        <strong class="error">{{ error }}</strong>
        <p data-ng-hide="addMode"><a data-ng-click="toggleAdd()" href="javascript:;" class="btn btn-primary">Add New</a></p>
        <form name="addComplant" data-ng-show="addMode" style="width:600px;margin:0px auto;">
            <div class="form-group">
                <label for="cid" class="col-sm-2 control-label">ID:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="cid" placeholder="please enter id" data-ng-model="newcomplaint.FileNum" required />
                </div>
            </div>
            <div class="form-group">
                <label for="cname" class="col-sm-2 control-label">Name:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="cname" placeholder="please enter your name" data-ng-model="newcomplaint.CHECKLISTs[0].IntIAB" />
                </div>
            </div>

            <br />
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <input type="submit" value="Add" data-ng-click="add()" class="btn btn-primary" />
                    <input type="button" value="Cancel" data-ng-click="toggleAdd()" class="btn btn-primary" />
                </div>
            </div>
            <br />
        </form>
    </div>
</div>

my console.log output:

console.log

Can someone tell me if I can display it using angular http.get and http.post, why can't I add into checklists field when it is clearly in the JSON file and it displays just fine? The error i get is [object object] ?

Thank you

2
  • console.log the data from the success handler and add it to the question. Commented Feb 24, 2016 at 18:57
  • @JohannesJander I have added it. Commented Feb 24, 2016 at 19:25

2 Answers 2

0

Just by looking at your code, it seems that you're pushing into $scope.complaints, instead of $scope.complaint.CHECKLISTs

Do you have an ng-repeat somewhere?

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

1 Comment

I do, i have added it into my html code under use angular. I though that $scope.complaints already has checklists in the json file?
0

Change $scope.complaints.push(data); to $scope.newcomplaint = data. Or if complaints really should be an array, then you need an ng-repeat on your form

6 Comments

Hi Johannes, I do have a ng-repeat on my page but not inside of the add new complaint form. I also changed the $scope.complaints.push(data); to $scope.newcomplaint = data which did not fix the error [object Object] but still worked.
Just console.log the data from the error handler and see what's inside.
The error in the error handler says :: "Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.ICollection`1[CRAMSMVC.Models.CHECKLIST]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
↵To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. ↵Path 'CHECKLISTs.0', line 1, position 35." What would you suggest? I am still learning and don't want to mess up my code.
"mess up your code" - that's really not an argument. As a coder, you mess up your code everyday to find a problem and then you tidy it up again. Try $http.post('/api/Complaint/', [this.newcomplaint]).success(function (data) { - that error comes from your .NET backend, so I have no idea why it is thrown.
|

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.