Retrieve Data From Controller To View

Get Data From Controller To View (Beta)

Here we are going to learn that how to get the data from controller to view. We already learnt that how to send the data from view to controller. If you are new to this blog then I suggest you to refer previous articles Post data from controller to view, start with first mvc 5 application.

Get the form data from controller

First we have to create a model that will used to transfer data from controller to view. Here i created a model class DemoModel. It has three properties Name, Email and Mobile.

Model

namespace FirstMvcApp.Models

{

    public class DemoModel

    {

        public string Name { get; set ; }

        public string EmailId { get; set ; }

        public string Mobile { get; set ; }

    }

}

Here I am sending the dummy data from controller to view using model class DemoModel. I create a object of DemoModel class inside index method. I set the data in each properties of the model class. Finally while returning the view, return the model with the view like below codes:

Controller


using
 System;

using System.Web.Mvc;

using FirstMvcApp.Models;

 

namespace FirstMvcApp.Controllers

{

    public class DemoController : Controller

    {

        // GET: Demo

        public ActionResult Index()

        {

            DemoModel demo = new DemoModel();

            demo.Name = "ProgramIdea.com";

            demo.EmailId = "info@ProgramIdea.com";

            demo.Mobile = "9876543210";

            return View(demo);

        }       

    }

}

For getting the data from controller to view, first you have to define the model on top of the page that returned from controller. In above controller codes, we are returning DemoModel class object, so we defined DemoModel class a model on view. We created strongly typed view for getting the value from controller.

View

<!DOCTYPE html>

@model< span class="Apple-converted-space">  FirstMvcApp.Models.< span style="font-size: 9.5pt; font-family: Consolas; color: rgb(43, 145, 175); background: white;">DemoModel< o:p>

<html>

<head>

    <meta name="viewport" content="width=device-width" />

    <title>Index</title>

    <link href="~/Content/bootstrap.css" rel="stylesheet" />

</head>

<body>

    <div>

        @using (Html.BeginForm())

        {

            <div class="form-group">

                <label>Name</label>

                @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })

            </div>

 

            <div class="form-group">

                <label>Email Id</label>

                @Html.TextBoxFor(m => m.EmailId, new { @class = "form-control" })

            </div>

 

            <div class="form-group">

                <label>Mobile</label>

                @Html.TextBoxFor(m => m.Mobile, new { @class = "form-control" })

            </div>

 

            <button type="submit" class ="btn btn-primary"> Save</button>

        }

    </div>

</body>

</html>

Demo

demoimage

Get the data list from controller

Here we are going to retreive the list of records.

using System;

using System.Web.Mvc;

using FirstMvcApp.Models;

using System.Collections.Generic;

 

namespace FirstMvcApp.Controllers

{

    public class DemoController : Controller

    {

        // GET: Demo

       

        public ActionResult StudentList()

        {

            List<DemoModel> demoList = new List<DemoModel>();

            DemoModel demo;

 

            demo = new DemoModel();

            demo.Name = "Vimal Kumar";

            demo.EmailId = "info@ProgramIdea.com";

            demo.Mobile = "9512345678";

            demoList.Add(demo);

 

            demo = new DemoModel();

            demo.Name = "Jitesh Byahut";

            demo.EmailId = "info@ProgramIdea.com";

            demo.Mobile = "9876543210";

            demoList.Add(demo);

 

            demo = new DemoModel();

            demo.Name = "Sonu Kumar";

            demo.EmailId = "info@ProgramIdea.com";

            demo.Mobile = "9812347650";

            demoList.Add(demo);

 

            demo = new DemoModel();

            demo.Name = "Amit Kumar";

            demo.EmailId = "info@ProgramIdea.com";

            demo.Mobile = "7812340568";

            demoList.Add(demo);

 

            return View(demoList);

        }

    }

}

@model  IEnumerable < span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"><FirstMvcApp.Models. DemoModel < span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;">>

 

<!DOCTYPE html>

<html>

<head>

    <meta name="viewport" content="width=device-width" />

    <title>Student List</title>

</head>

<body>

    <table class="table table-bordered">

        <tr>

            <th> @Html.DisplayNameFor(model => model.Name) </th>

            <th> @Html.DisplayNameFor(model => model.EmailId) </th>

            <th> @Html.DisplayNameFor(model => model.Mobile) </th>

        </tr>

 

        @foreach (var item in Model)

        {

            <tr>

                <td> @Html.DisplayFor(modelItem => item.Name) </td>

                <td> @Html.DisplayFor(modelItem => item.EmailId) </td>

                <td> @Html.DisplayFor(modelItem => item.Mobile) </td>

            </tr>

        }

    </table>

</body>

</html>

Demo

demoimage