articles

Home / DeveloperSection / Articles / Strongly type View In Mvc

Strongly type View In Mvc

Manish Kumar4257 12-Jan-2017

The View which Bind with any model is called Strongly Typed View.  You Can bind any class  as model to view

For Binding any Model Class in View we use namespace @using Myproject.Models.ModelClass.

In View. After that you can access property of that model.

Example

First make a model Class

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace MvcApplication1.Models
{
    publicclass Mindstick
    {
        publicstring Name { get; set; }
        publicstring Mobile { get; set; }
        publicstring Address { get; set; }
    }
}

Then Add Controller and make object of model class inside Index action Method
using MvcApplication1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
 
 
namespace MvcApplication1.Controllers
{
    publicclassHomeController : Controller
    {
        //
        // GET: /Home/
 
        publicActionResult Index()
        {
            Mindstickobj = newMindstick();
            obj.Name = "Mindstick";
            obj.Address = "Allahabad";
            obj.Mobile = "1234567890";
                       return View(obj);
                      
        }
        publicActionResult About()
        {
            return View();
        }
 
    }
}


Now Add View for action Method Index
@model  MvcApplication1.Models.Emp
@{
    Layout = null;
}
 
<!DOCTYPEhtml>
 
<html>
<head>
    <metaname="viewport"content="width=device-width"/>
    <title>Index</title>
</head>
<body>
 
    <div>
      @Html.DisplayFor(m=>m.Name);<br/>
        @Html.DisplayFor(m =>m.Mobile);<br/>
        @Html.DisplayFor(m =>m.Address);<br/>
           </div>
 
</body>
</html>

Output


Strongly type View In Mvc

You can also check this helpful link

Strongly Typed View in MVC


Updated 07-Sep-2019

Leave Comment

Comments

Liked By