Z 9 ) STRONGLY TYPED VIEW IN ASP.NET MVC

 STRONGLY TYPED VIEW IN ASP.NET MVC

Ø Strongly typed view or strongly typed object is used to pass data from controller to a view.

Ø The view which binds with any model is called as strongly typed view.

Ø You can bind any class as a model to view.

Ø You can access model properties on that view.

Ø You can use data associated with model to render controls.

Ø The view that is designed by targeting specific model class object then that view is called "Strongly Typed View". In strongly typed view, view is bind with corresponding model class object or list of objects.

There are several ways available to pass data from a controller to a view in an MVC application.

1. ViewBag or ViewData

2. Dynamic Type

3. Strongly typed view

Benefits of using strongly typed views.

· Compile time error checking

· Intellisense


    Codes :-

Model Class :-


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;


namespace Strongly_Type_view.Models

{

    public class emp

    {

        public int id { get; set; }

        public string name { get; set; }

        public string city { get; set; }

    }

}


Controlller :- 


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using Strongly_Type_view.Models;


namespace Strongly_Type_view.Controllers

{

    public class HomeController : Controller

    {

        public ActionResult Index()

        {

            emp e1 = new emp();


            e1.id = 1;

            e1.name = "Sourabh";

            e1.city = "Nagpur";


            emp e2 = new emp();

            e2.id = 2;

            e2.name = "Satya Nadela";

            e2.city = "Delhi";


            emp e3 = new emp();

            e3.id = 3;

            e3.name = "Kumar Sanu";

            e3.city = "Aurangabad";


            List<emp> emplist = new List<emp>();

            emplist.Add(e1);

            emplist.Add(e2);

            emplist.Add(e3);


            ViewData["var1key"] = e1;

            ViewBag.var2key = e1;


            return View(emplist);

        }

    }

}


View :-


@model List<Strongly_Type_view.Models.emp>

@*@model Strongly_Type_view.Models.emp*@


@{

    ViewBag.Title = "Index";

}


<h2>Index</h2>

@{

    var a = (Strongly_Type_view.Models.emp)ViewData["var1key"];

    var b = ViewBag.var2key;

}


<h2>ViewData using view  data</h2>    Viewdata show the comppile  time erroor

<h3>@a.id</h3>

<h3>@a.name</h3>

<h3>@a.city</h3>


<br />


<h2>Viewbag using view data</h2>  viewbag do not show the compile time error ,view bag is show the run time error

<h3>@b.id</h3>

<h3>@b.name</h3>

<h3>@b.city</h3>


<br />


@*<h2>Model Using view data</h2>

<h3>@Model.id</h3>

<h3>@Model.name</h3>

<h3>@Model.city</h3>*@


<br />


<h2>View Data using STRONGLY TYPE VIEW </h2>

<ul>


        @{

            foreach (var i in Model)

            {

                <h3>@i.id</h3>

                <h3>@i.name</h3>

                <h3>@i.city</h3>

            }

        }

    </ul>



OutPut :-


                


















 

Comments

Popular posts from this blog

DATA CONTROL ( Gridview , Repeater , Formview , DataList , Detailsview , Listview )

Z 5 ) MVC (Model view Controller)

Z 8 ) MVC HTMLHELPER METHOD AND ACTIONLINK (how to use css ,atrributes,style in css)