0

I have a Webapi controller as below . Pretty crude though .

public async Task<HttpResponseMessage> Put(string id, [FromBody]Students studentToupdate)
    {
        if (!ModelState.IsValid)
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest, "Model state Not valid");
        }
        if (studentToupdate == null)
            return new HttpResponseMessage(HttpStatusCode.BadRequest);
        Students student = await Context.Set<Students>().FindAsync(new Guid(id));
        student.FirstName = studentToupdate.FirstName;
        student.LastName = studentToupdate.LastName;
        student.Address = studentToupdate.Address;
        student.DOB = studentToupdate.DOB;
        student.Phone = studentToupdate.Phone;

        Context.Entry(student).State = EntityState.Modified;
        try
        {
            int result = await Context.SaveChangesAsync();
            return result > 0 ? Request.CreateResponse<Students>(HttpStatusCode.OK, student) : Request.CreateResponse(HttpStatusCode.InternalServerError);
        }
        catch (Exception ex)
        {
            return Request.CreateResponse<Exception>(ex);
        }
    }

My angular controlller is something like this .Its in coffeescript .

SiteAngular.Student.controller 'Edit',['$scope','$state','$stateParams','Factory',($scope,$state,$stateParams,factory)->
id=$stateParams.id
student = factory.get id:id
$scope.student=student

$scope.Update = (id,student)->
    factory.update
        id:id
        studentToupdate:student
    ]

And last not the least my html is likely to be a list and its button has a update method as :

<div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="button" value="Save" class="btn btn-default" ng-click="Update(student.StudentGuid,student)"/>
        </div>
    </div>

above the div I have ng-repeat to looped into table for various properties for student . and in the update method I want to send the Guid and the student object to mentioned webapi controller

The problem is this in webapi controller string id received the guid perfectly . However all the properties of studentToUpdate is null . I checked the header its perfectly to be like

Accept  application/json, text/plain, */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Length  250
Content-Type    application/json;charset=utf-8
Cookie  __RequestVerificationToken=SBC0HZDwZOEyOfG0dMN0Da-MqFYQoVoiaG3_LIpnXc5a4tSsu1wP4U8Qy3cgrw7w_rMmDu5577pVrZ0Kmzo9YCZcLvFY93f37Heat160h6k1
Host    localhost:3090
Referer http://localhost:3090/
User-Agent  Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0

Can somebody tell me why the id gets populated and not the FromBody student object . The properties of student object in webapi put method is all null??

using webapi v5.1.0.0. Any clues ?

Joy
  • 6,438
  • 8
  • 44
  • 75

1 Answers1

0

I have the answer . The update put should have been likely to be

$scope.Update = (id,student)->
factory.update
    id:id
    student
]

The parameter key studentToupdateisn't required it automatically gets resolved while getting passed as jquery json object .

Joy
  • 6,438
  • 8
  • 44
  • 75