JSON Result in Controller Sample in MVC

JSON Result in Controller Sample in MVC

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...".

image.png

Step 2

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

Click on the "Add" button.

image.png

Step 3

Name the controller as in the following:

image.png

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

image.png

Step 4

Now we need to create a view.

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

image.png

Step 5

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

Click on the "Add" button.

image.png

Step 6

Add a title in the index page.

image.png

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.

image.png

Step 8

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

image.png

Step 9

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

image.png

<< Day 12

>>Day 14