How to modify data of particular column of table in database using WEB-API in MVC 4.0?

If you are new to PUT operation of WEB-API in MVC 4.0 then this code will guide you while creating MVC 4.0 application. I am using visual studio ultimate edition 2012. You can post your queries here incase you have. It would be great to help you.

Description of the concept - I am using entity framework .edmx file in my WEB-API MVC 4.0 application.
I want to modify data of particular column of table in database using API controller.
Verbs -
As we know there are different verbs in MVC 4.0 GET , POST, PUT, DELETE used for diiferent operations.
For modify/update operation , PUT is used.
How to create api controller?
If you right click on controllers, you will find menu to add controller, at the time of creation , it will ask you whether to create MVC controller or API controller.
You should choose API with empty read write action templates so that you can place following code under PUT.

WEB-API WEB-API is a very powerful template in MVC4.0. Request and responses are sent to-fro between client and server through web api.
I have choose Razor engine at the time of application making.

private yourentityname db = new yourentityname ();

Make sure entity name should be matched with entity name present in config file. At the time of creation of edmx file it gets saved.

public HttpResponseMessage Put (int id, Tablename tblObject)
if (ModelState.IsValid && id == tblObject.columnname)
db.Entry(tblObject).State = EntityState.Modified;
catch (DbUpdateConcurrencyException)
return Request.CreateErrorResponse(HttpStatusCode.NotFound , "Error found.");
return Request.CreateResponse(HttpStatusCode.OK);
return Request.CreateErrorResponse(HttpStatusCode.BadRequest , "Bad request.");

How to test this code?
You can test this code by using any web debugging tool say fiddler.
Its a good practice to host your web application in IIS and then the uri should be given for fiddler so that there won't be port issues.
HttpResponseMessage status code is generated based on success or failure.
In fiddler, double click on uri after execution and see JSON created as output.


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: