Different Ways To Pass Data From Controller To View In MVC

Asp.net-core By Sagar Jaybhay

Different ways to pass data from controller to view in asp.net core By Sagar Jaybhay

There are different ways to pass data from controller to view in asp.net core mvc which are ViewData, ViewBag, and Strongly-typed view. Below is the first way to Pass Data From Controller To View in ASP.NET CORE .

ViewData:

We pass data in ViewData which is a dictionary. In this, we store information in a key-value manner. We set value in

ViewData["NewTitle"] = "Student Information";

We set data as above and we access this In specified view. To retrieve this in view we use.

@ViewData["NewTitle"]

If we use this we don’t have intellisense and we can easily misspell this. So if you in such a situation you will only this buy run time we can’t get at compile time. If you made such mistake page crashes.

Features Of ViewData

  1. Is a dictionary of weak-data type.
  2. It uses string value as the key to retrieve data
  3. It dynamically resolves at run time
  4. Also, it doesn’t have compile-time checking and IntelliSense

Below is the second way to Pass Data From Controller To View in ASP.NET CORE .

ViewBag

We can pass the data from controller to view.

ViewBag.DynamicPropertyName=” your object”;

In this, you can assign a dynamic property name with value. But in this also we don’t have intellisense and you can easily misspelled name or key of that. If you assign any object to ViewBag then their properties easily resolve. No need to explicitly cast that.

ViewBag.Student= studentObject;

So in a view we can get ViewBag.Student.PropertyName

What is the difference between ViewBag and ViewData?

  1. ViewBag is a wrapper around ViewData
  2. Both of these will create a loosely coupled view
  3. ViewData uses string keys to store and retrieve data
  4. ViewBag used dynamic properties which will resolve at run-time
  5. Both of these resolve at run-time
  6. Both don’t have compile-time checking

Below is the Third way to Pass Data From Controller To View in ASP.NET CORE .

Strongly Typed View

To specify a strongly typed view we need to use @model type in the view at first line.

@model LearnAspCore.Models.Student
  1. It is strongly typed so we get intellisense.
  2. Typing error easily avoid.
  3. Compile-time type checking is available.
  4. To access data we need below code like this.
@Html.DisplayNameFor(model => model.StudentId)

Below is the fourth way to Pass Data From Controller To View in ASP.NET CORE .

ViewModel

  1. In some situation, our model does not contain all the data which we need, so for that reason we create ViewModel and it will contain all data our need.
  2. For that reason, we need to combine multiple class or source data into one and pass it to view. So for that reason, we create viewmodel. General naming rule initial of the controller after that method and suffix VM in our case home controller details method so the name becomes HmDetailsVM.
   public class HmDetailsVM
    {
        public Student student { get; set; }

        public string DivisonOfStudent { get; set; }
    }

Below is code for view

@*@model LearnAspCore.Models.Student*@

@model LearnAspCore.ViewModel.HmDetailsVM

@{
    ViewData["Title"] = ViewData["NewTitle"];
}

@*<h1>@ViewData["NewTitle"]</h1>*@

    <h1>@ViewBag.TitleNew</h1>

<h4>@Model.DivisonOfStudent</h4>

<div>
    <h4>Student</h4>
    <hr />
    <dl class="row">
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.student.StudentId)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.student.StudentId)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.student.FullName)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.student.FullName)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.student.Address)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.student.Address)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.student.Division)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.student.Division)
        </dd>
    </dl>
</div>
<div>
    @Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
    <a asp-action="Index">Back to List</a>
</div>
ViewModel in Asp.net core to pass data from controller to view

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Related posts