Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys, I have this controller: (It's the default when you create a project with mvc)

[ResponseType(typeof(Event))]
        public async Task<IHttpActionResult> PostEvent(Event @event)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Events.Add(@event);
            await db.SaveChangesAsync();

            return CreatedAtRoute("DefaultApi", new { id = @event.Id }, @event);
        }


What I want to do is to able to post/put into the DB using JSON any tips on how to do this?
Posted
Comments
Afzaal Ahmad Zeeshan 5-Feb-15 6:30am    
Where is that JSON data, are you passing that data to this action somewhere?
deepankarbhatnagar 5-Feb-15 7:36am    
please clarify..

1 solution

hi ,you will use a repository pattern and implement it in your controller (Mvc or Api)
for example with Controller Mvc use this code :
************************************************************
// POST: /Persons/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<actionresult> Create(Person person)
{
if (ModelState.IsValid)
{
await _genericRepository.InsertAsync(person);
return RedirectToAction("Index");
}

return View(person);
}
**************************************************************
be careful i am used a folder that contains two files concerning Repository pattern if you want i will send you my code source
then you will download a Postman Chrome Rest client and choose Post method to create what you want.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900