JSON Result in Controller Sample in MVC: Day 13

Introduction

This article shows how to use a JSON result in a controller in MVC.

Step 1

Create a MVC project from the "Empty" template.

Right-click on "Controllers" and select "Add" >> "Controller...".



Step 2


Select "MVC 5 Controller - Empty" to add an empty controller.

Click on the "Add" button.



Step 3

Name the controller as in the following:



And it will add an action result method to the controller.



Step 4

Now we need to create a view.

Right-click on "Index" and select "Add View...".



Step 5

Name the view and select "Empty (without model)" as the template.

Click on the "Add" button.



Step 6

Add a title in the index page.



Step 7

JsonResult is used to send JSON formatted content to the response. Create an object of employee inside the jsonresult method and return a jsonresult object. Commented code represents another way to create a jsonresult object.



Step 8

Create a link to call the "jsonResult" method in index.cshtml.



Step 9

Run the project, click on the "JsonResult" link and you can see the JSON data rendered on the browser.



<< Day 12                                                                 >> Day 14


Similar Articles