Skip to content

Store json data object to database in asp net mvc entity fra

Pabitra Pradhan

Asked Mar 13, 2015, 2:12 AM EDT1 answer
Share
LinkedInX
hi ,
 
i have json data as shown below and i want to store it in to existing database  having  predefined schema  and  i am using MVC 4 and this json result is returned by web service.so  can some one help me how to do that.
 
Advance Thanx 
 
{ 'store':[
{
"StoreID": "1",
"StoreName": "Victoria CWH",
"BrandID": 2,
}
]}
 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Create a action method in ur corresopnding controller e.g public ActionResult StoreData(StoreModel model)
    decorate ur method with [HttpPost] attribute.
    And finally make AJAX Jquery post call to ur Action method along with the json object.

     var store = { 'store':[
    {
    "StoreID": "1",
    "StoreName": "Victoria CWH",
    "BrandID": 2,
    "ServerHostName": "http://demo3kentico8.raybiztech.com";
    }
    ]}

      $.ajax(
      {
      type: "POST",
      url: "/controller/action",
      data: store,
      contentType: "application/json; charset=utf-8",
      async: false,
     
      },
      success: function (data) {
      alert('success')
      },
      error: function () {
      alert("Error occured while getting data from server.");
      }
      }
      );

    Then write your data strore logic using EF at newly created Action method.

    public ActionResult StoreData(StoreModel model)
    {

    //use model object to store the data in db using EF.

    return Json(new {
    success=true;
    });
    }
    0