http patch to only update certain fieldshttp://forums.asp.net/t/1797232.aspx/1?http+patch+to+only+update+certain+fieldsThu, 26 Apr 2012 12:29:46 -040017972324952568http://forums.asp.net/p/1797232/4952568.aspx/1?http+patch+to+only+update+certain+fieldshttp patch to only update certain fields <p>Can i use http patch with the web api to update only&nbsp;certain fields from my entity model ?<br> Can someone possibly provide some pseudo code or maybe a working example ?</p> <p>If i can't use patch what is the common alternative aproach to update only certain fields with the web api ?&nbsp;</p> 2012-04-26T10:22:52-04:004952702http://forums.asp.net/p/1797232/4952702.aspx/1?Re+http+patch+to+only+update+certain+fieldsRe: http patch to only update certain fields <p>According to this&nbsp;<a href="http://aspnetwebstack.codeplex.com/workitem/3">http://aspnetwebstack.codeplex.com/workitem/3</a>&nbsp;, Web API already supports PATCH verb.</p> <p>According to&nbsp;<a href="http://aspnetwebstack.codeplex.com/discussions/350398">http://aspnetwebstack.codeplex.com/discussions/350398</a>&nbsp;,&nbsp;Web API supports&nbsp;AcceptVerbsAttribute so you can define PATCH using the attribute.</p> <p>This works (although actually does not what PATCH is suppose to do but my point is PATCH verb already supported):</p> <p>[AcceptVerbs(&quot;PATCH&quot;)]<br> public string Patch()<br> {<br> return &quot;Patch!&quot;;<br> }</p> <p>In terms of functionality, you need to:</p> <ol> <li>Define method so that it accepts PATCH </li><li>Define parameter representing your model and id of the parent </li><li>using id of the parent get the parent model </li><li>update the parent model using the model passed in </li><li>Save back to database </li></ol> 2012-04-26T11:11:54-04:004952711http://forums.asp.net/p/1797232/4952711.aspx/1?Re+http+patch+to+only+update+certain+fieldsRe: http patch to only update certain fields <p>Thank you aliostad, i took a look at those documents, i'm not sure however how to exactly implement that.<br> Could you provide some very simple pseude code , i am not sure what the api controller method should look like.&nbsp;</p> 2012-04-26T11:16:26-04:004952712http://forums.asp.net/p/1797232/4952712.aspx/1?Re+http+patch+to+only+update+certain+fieldsRe: http patch to only update certain fields <p>Please see my updates.</p> 2012-04-26T11:16:57-04:004952763http://forums.asp.net/p/1797232/4952763.aspx/1?Re+http+patch+to+only+update+certain+fieldsRe: http patch to only update certain fields <p>Thank you for your exmple, i'm still not completly clear on how to use this. Allow me to demonstrate.</p> <p>Assume i have this model</p> <pre class="prettyprint">public partial class Todo { public int id { get; set; } public string content { get; set; } public bool done { get; set; } }</pre> <p>And i send this as json data to my controller as a patch request. <br />This is mearly the action of toggeling a checkbox.&nbsp;<br />I think it makes sence that i only want to sent that to my server, and not the entire model.</p> <pre class="prettyprint">{ "id":1, "done" : true }</pre> <p>This is my controller</p> <pre class="prettyprint">[AcceptVerbs("PATCH")] public string Patch( Todo todo ) { /* How do i determine wich JSON values where submitted ? How do i send only the submitted JSON values to the database ? */ }</pre> <p>It seems like a very basic thing to do, but i can't seem to get it right !<br> Thank you for your time.</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> 2012-04-26T11:39:14-04:004952870http://forums.asp.net/p/1797232/4952870.aspx/1?Re+http+patch+to+only+update+certain+fieldsRe: http patch to only update certain fields <p>ASP.NET Web API seems to be missing `UpdateModel`, `TryUpdateModel`, etc.</p> <p>In ASP.NET MVC, you could use them to achieve the desired effect. I have created a work item in ASP.NET Web Stack which you can vote for.</p> <p><br> Work item: http://aspnetwebstack.codeplex.com/workitem/102</p> 2012-04-26T12:29:46-04:00