jeudi 30 mars 2017

Update web api record with knockout.js

I have this method in controller:

// PUT: api/Books/5
        [ResponseType(typeof(void))]
        public async Task<IHttpActionResult> PutBook(int id, Book book)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != book.Id)
            {
                return BadRequest();
            }

            db.Entry(book).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BookExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

I have this method in Knockout.js

self.PutBook = function (item, book) {
        ajaxHelper(booksUri + item.Id, 'PUT').done(function (item) {
            self.books.put(item, book);

        });
    }

I want to update record with values that are entered in frontend input fields. What is missing in knockout.js? I'm not able to solve it.




Aucun commentaire:

Enregistrer un commentaire