0

I’m using Angularjs and asp.net mvc 5 with webApi2.

I’m having some trouble calling a custom PUT method. I’ve done some studying for the past few days, and although I have a decent feel for the situation, I can’t get my JSON object to pass as a parameter for some reason.

Route template:

routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }

Web api controller and model (shortened for brevity):

public class AttModel
{
    public string dc { get; set; }
    public string dt { get; set; }
}

[HttpPut]
public IHttpActionResult PutAttendRecord([FromBody]AttModel model)
{
    string dc = model.dc;
    DateTime dt = Convert.ToDateTime(model.dt);
        
    var record = (from tbl in db.attend_am_y1
                 where tbl.dc_number == dc && tbl.class_date_am == dt
                 select tbl).SingleOrDefault(); 

    record.status_am = "z";
    db.SaveChanges();
}

Javascript object (angularjs PUT):

$scope.updateRecord = function () {
        
    var stuInfo = {
        dc: $scope.student.dc,
        dt: $scope.student.dt
    };

    $http.put("/api/attendance/PutAttendRecord/" + stuInfo)
        .then(function (d) {
            alert(d.data.dc_number);
        });
}

I tried using Newtonsoft without the extra AttModel class, and passing the param as jObject, but I still get a null value exception within the iHttpActionResult method. The data just isn’t making it to my method. Routing issue?

If I manually place values within these variables in the iHttpActionResult, the method works fine.

1 Answer 1

2

Assuming you are getting into your call alright, you want to attach your object in the body

     $http.put("/api/attendance/PutAttendRecord/", stuInfo)
    .then(function (d) {
        alert(d.data.dc_number);
    });

and I don't think you need [FromBody] as I believe this is only specified if the function finds it unclear.

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

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.