4

Inside my WebAPI asp.net mvc controller Delete method, the passed in object called contact is coming out to be null. I have checked my code every where and I can not figure out the root cause. I have edit operation working successfully in a very similar fashion.

So what is causing the contact object parameter inside the webapi asp.net method to come in as null value?

I have checked as shown in diagram that the contact object inside the angular controller is not null before being passed into the webapi delete method.

enter image description here

Here is my rest of the code 

  <div data-ng-controller="ContactDeleteController">
        <form name ="deleteContact" data-ng-submit="saveDeleteContact()">
            <div>
                <label>First Name: </label>
                <input required type="text" placeholder="Enter First Name" data-ng-model="contact.FirstName"/>
            </div>
            <div>
                <label>Last Name: </label>
                <input required type="text" placeholder="Enter Last Name" data-ng-model="contact.LastName"/>
            </div>
            <div>
                <label>Email Address: </label>
                <input required type="text" placeholder="Enter Email Address" data-ng-model="contact.EmailAddress"/>
            </div>
            <div>
                <label>Cell Phone Number: </label>
                <input required type="text" placeholder="Enter Phone Number" data-ng-model="contact.PhoneNumber"/>
            </div>
            <div></div>
            <div>
                <button class="btn btn-primary" type="submit">Delete</button>
            </div>
        </form>
    </div>



    var ContactDeleteController = function ($scope, $http, $location) {
        var contactId = $location.absUrl().match(/\/Delete\/(.*)/)[1];
        $http.get("/api/ContactWeb/" + contactId)
            .then(function (response) {
                $scope.contact = response.data;
            });

        $scope.saveDeleteContact = function () {
            var con = $scope.contact;
            $http.delete("/api/ContactWeb", con)
                .then(function (response) {
                    $scope.contact = response.data;
                });
            window.location = "/Contact/Index";
        };
    };

enter image description here

2
  • Instead of attaching images, it would be more helpful if you could just copy and paste your code. The image is not visible. Also, people cannot copy it. Commented Oct 25, 2014 at 0:38
  • can you try commenting out the window.location="/Contact/Index" line and see what happens. Wondering if the fact that it's being called right after $http is setup (rather than inside the .then block when $http has completed) is causing a race condition that is keeping $http from completing properly. Commented Oct 25, 2014 at 2:14

2 Answers 2

8

HTTP does not allow DELETE with body. Try sending the parameters in URI (query string, etc) and in Web API, bind the data into the complex type Contact using [FromUri].

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

1 Comment

HTTP does not forbid DELETE with body. One use case of DELETE with body is when the length of the contents exceeds the allowed length of query string. The real question is if the client, the server and all "hops" between them will honor and preserve the body.
3

Just send the id of the entity to the server in your request and you don't need to include any data in the body. now on the server you can delete the entity with that id.

2 Comments

what if the entity have a composite key, meaning more than 1 id params required?
Send more than one parameter?

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.