{ "Name": "Team Name", "Lat": 29.723169, "Lng": -95.514999, "ShiftEndTime": 1000, "ShiftStartTime": 0 }
And the following class is your Model.
public class TeamViewModel { public long TeamId { get; set; } public string Name { get; set; } public double Lat { get; set; } public double Lng { get; set; } public TimeSpan ShiftStartTime { get; set; } public TimeSpan ShiftEndTime { get; set; } }
Refer image and see highlighted yellow mark
1. Select POST method
2. Highlighted body section and selected raw data
3. selected raw data with "Application/JSON" as type
4. Underbody selection writes your JSON object
- Generally, this method is used in most in normal cases so we can see in MVC this is the default case to get JSON POST data from the body as a parameter.
public ActionResult Index(TeamViewModel model) { //TODO //Write your code here }
You can see here TeamViewModel pass as a parameter and then utilize for further logic.
2. From Request Object
- Generally, this kind of cases used when directly post request from other pages or same kind of requirement.
public ActionResult Index() { Stream req = Request.InputStream; req.Seek(0, System.IO.SeekOrigin.Begin); string json = new StreamReader(req).ReadToEnd(); PlanViewModel plan = null; try { // assuming JSON.net/Newtonsoft library from http://json.codeplex.com/ plan = JsonConvert.DeserializeObject<TeamViewModel>(json); } catch (Exception ex) { // Try and handle malformed POST body return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } //TODO:: you can write your logic here }
You can see the code, get JSON POST data from the Request object. Once you read StreadReader object and converted into the string.
Then string will convert into TeamViewModel object help of JsonConvert.DeserializeObject method and finally get the TeamViewModel object.
Hope you enjoy this post and learn something.
Please post your suggestion in comment section.