Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
i'm trying to post model to a web api where every time only one item in the model shown up at post method. like if i'm posting a model called Movie ,Id and Title are the properties of this model.
so here my problem is when i'm trying to post it from client side using knockout.js,only Title value is coming in the web api post method.


post method in api
C#
[HttpPost]
        [ODataRoute("CreateMovie")]
        public IHttpActionResult CreateMovie(Movie parameters)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            string title = "Test";//parameters["Title"] as string;

            Movie movie = new Movie()
            {
                Title = title,
                ID = _db.Movies.Count + 1,
            };

            _db.Movies.Add(movie);

            return Created(movie);
        }

client side code:
JavaScript
window.viewModel = (function () {
    var self = this;

    self.movies = ko.observableArray();
    self.id = ko.observable();
    self.title = ko.observable();
    self.year = ko.observable();
    self.duedate = ko.observable();
    self.ischeckedout = ko.observable();
    self.errorMessage = ko.observable();

    // Function to invoke the non-bindable "CreateMovie" action.
    self.createMovie = function () {
        var movieVal = { ID: self.id(), Title: self.title(), Year: self.year(), DueDate: null, IsCheckedOut: self.ischeckedout() };
        ajaxRequest("post", "odata/CreateMovie",movieVal)
            .done(function (result) {
                self.movies.push(new movie(result))
            })
            .fail(function (jqXHR, textStatus, errorThrown) {
                self.errorMessage(errorThrown);
            });
    }    // Ajax helper
    function ajaxRequest(type, url, data) {
        debugger;
        var options = {
            dataType: "json",
            contentType: "application/json",
            type: type,
            data: data ? ko.toJSON(data) : null
        };
        self.errorMessage(null); // clear error message
        return $.ajax(url, options);
    }
})();

$(document).ready(function () {
    ko.applyBindings(viewModel);
});



So in web api post methdo only Title is showing up every time not all other items.

suggest me on this.


Thanks in advance
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900