I created a simple to-do list application in asp.net mvc 3 with web api and dbContext. (with foundation and requirejs for the client) Everything works fine, but I'm kind of concerned about the fact that I need to send the entire model to the server if I check or uncheck how to do this. I would only like to send the "done" field when sending the data.
I should mention that I also use JsonNetFormatter to use JSON.NET as a standard Serializer (explained here: http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net- with-asp-net-web-api.aspx ).
This is currently my api controller method for updating the model
public HttpResponseMessage Put(Todo todo) { _db.Entry(todo).State = EntityState.Modified; _db.SaveChanges(); return new HttpResponseMessage(HttpStatusCode.NoContent); }
It takes this as json data
{"content":"Pick up milk","done":false,"id":10}
Of course, this works, but it updates the whole model, it should update only one field. I can only achieve sending the changed fields to the server from the browser, but I'm not sure what the web api method should look like. I was thinking about doing something with FormCollection, but this does not seem to work with the web api, since it seems to be trying to serialize the passed formal values โโdirectly to the FormCollection type, I get this error.
Cannot deserialize JSON object (ie {"name":"value"}) into type 'System.Web.Mvc.FormCollection'.
How can I send a partial update for 1 or more fields from a model to my web api? I just want to send the updated fields to the server, and from there only update these fields in the database. Of course, I do not want to query the database before updating.
Willem d'haeseleer
source share