I have an MVC 5 project that's using knockout and Web Api 2.
I have paging implemented with my knockout model being originally populated by the first page and the pagination along the bottom. Allowing the user to navigate to other pages within the result set.
The code is as follows:
Html markup that pushes the Photo Id to observable array when checkbox is checked:
<span>Add</span><input type="checkbox" class="photo" name="selected-photos" id="selected-photos" data-bind="checked:$root.selectedPhotos, checkedValue: $data.PhotoId" />
Knockout View Model
function FlickrPhotosViewModel() {
var self = this;
self.photos = ko.observableArray([]);
self.noOfPages = ko.observable();
self.selectedPhotos = ko.observableArray([]);
self.pageNumber = ko.observable(1);
self.selectPage = function(pageNumber) {
jQuery.support.cors = true;
$.ajax({
url: '@Url.RouteUrl("DefaultApi", new {httproute = "", controller = "PhotosFromFlickr"})',
type: 'GET',
data: { pageNumber: ko.toJSON(pageNumber), selectedPhotos: ko.toJSON(self.selectedPhotos()) },
dataType: 'json',
success: function(data) {
self.photos(ko.mapping.fromJS(data.Photos));
self.noOfPages(data.NoOfPages);
self.selectedPhotos(data.SelectedPhotos);
},
error: function(x, y, z) {
alert(x + '\n' + y + '\n' + z);
}
});
};
jQuery.support.cors = true;
$.ajax({
url: '@Url.RouteUrl("DefaultApi", new {httproute = "", controller = "PhotosFromFlickr"})',
type: 'GET',
data: { pageNumber: ko.toJSON(self.pageNumber()), selectedPhotos: ko.toJSON(self.selectedPhotos()) },
dataType: 'json',
success: function (data) {
self.photos(ko.mapping.fromJS(data.Photos));
self.noOfPages(data.NoOfPages);
},
error: function (x, y, z) {
alert(x + '\n' + y + '\n' + z);
}
});
};
$(document).ready(function () {
ko.applyBindings(new FlickrPhotosViewModel());
});
Web Api Get Method This currently take two arguments, first being the page number I want to serve up and the second being the selected photo ids, currently passed as a string.
public PhotoCollectionModel Get(int pageNumber, string selectedPhotos )
{}
The problem that I'm having is I want the second parameter to be a strongly typed. Or at the very least List selectedPhotos. Currently I'm having to deserialize the json string (selectedPhotos) in the Web Api Get method...
Is this possible or am I missing the point?
UPDATE
This is the request url it creates -
http://localhost/Mvc/api/PhotosFromFlickr?pageNumber=3&selectedPhotos=%5B%228060459369%22%2C%227895411674%22%2C%227067757145%22%5D
The Headers are:
Request Headers :
Accept:application/json, text/javascript, */*; q=0.01
Referer:http://localhost/Mvc/Profile?username=MarkHBrown
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36
X-Requested-With:XMLHttpRequest