Sunday, 29 September 2013

item is null using knockout and web api

item is null using knockout and web api

My knockout.js code is as follow
@section scripts {
<script type="text/javascript">
var serverValidationErrors = new Array();
function ViewModel() {
var self = this;
function VideoViewModel(user) {
var self = this;
self.Videod = ko.observable(video.RoleId);
self.Tile = ko.observable(video.Title);
self.Description = ko.observable(video.Description);
self.Link = ko.observable(video.Link);
self.RoleId = ko.observable(video.RoleId);
}
self.VideoInfo = ko.observable(); // Contains the list of
videos
self.Previous = ko.observable();
// save video information
self.save = function () {
alert('save clicked');
$.ajax({
url: 'http://localhost:51097/api/VideosApi/',
cache: false,
type: 'Post',
contentType: 'application/json; charset=utf-8',
data: ko.toJSON(self.VideoInfo()),
statusCode: {
201 /*Created*/: function (data) {
if (data) {
var viewModel =
ko.mapping.toJS(self.VideoInfo());
self.Previous(new VideoViewModel(viewModel));
alert('saved Successfully.')
}
},
400 /* BadRequest */: function (jqxhr) {
// debugger;
var validationResult =
$.parseJSON(jqxhr.responseText);
$.revalidate(validationResult.ModelState);
}
}
//self.getAll
})
.fail(
function (xhr, textStatus, err) {
});
}
}
var viewModel = new ViewModel();
ko.applyBindings(viewModel);
</script>
}
and my webapi controller add method is as follow .
public HttpResponseMessage PostVideo(video item)
{
if (ModelState.IsValid)
{
item = repository.Add(item);
var response =
Request.CreateResponse<video>(HttpStatusCode.Created,
item);
string uri = Url.Link("DefaultApi", new { id =
item.VideoId });
response.Headers.Location = new Uri(uri);
return response;
//return new HttpResponseMessage(HttpStatusCode.OK);
}
else
{
return
Request.CreateErrorResponse(HttpStatusCode.BadRequest,
ModelState);
}
}
the problem is in web api controller method video item is always null. I
am new to web api and knockout.js.

No comments:

Post a Comment