Binding a list (or collection) of objects (Model) to ASP.NET MVC View

by Jagadish Pulakhandam on 6/17/2012 8:16:34 PM
Rated 0 from 0 votes
Brief: Demonstrates on developing a View which gets bound to a list of objects (based on Model) in ASP.NET MVC
Posted to: Basic Tutorials/samples on ASP.NET MVC Programming
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Attached video demonstrates the following:
  • How to create a controller which returns a list (or collection) of objects (based on a Model) to View (in ASP.NET MVC)
  • How to create a View to bind to list of objects (in ASP.NET MVC)
  • How to iterate through a list of objects in ASP.NET MVC markup

Screenshot:


Source code:

Model:

01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Web;
05. 
06.namespace MvcApplication1.Models
07.{
08.    public class EmployeeInfo
09.    {
10.        public int Empno { get; set; }
11.        public string Ename { get; set; }
12.    }
13.}

Controller:

01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Web;
05.using System.Web.Mvc;
06.using MvcApplication1.Models;
07. 
08.namespace MvcApplication1.Controllers
09.{
10.    public class EmployeeController : Controller
11.    {
12.        //
13.        // GET: /Employee/
14. 
15.        public ActionResult Index()
16.        {
17.            List<EmployeeInfo> lstEmp = new List<EmployeeInfo>() {
18.                new EmployeeInfo() {Empno = 1001, Ename="Jag" },
19.                new EmployeeInfo() {Empno = 1002, Ename="Chat" },
20.                new EmployeeInfo() {Empno = 1003, Ename="Win" },
21.                new EmployeeInfo() {Empno = 1004, Ename="Dhan" }
22.            };
23.            return View(lstEmp);
24.        }
25. 
26.    }
27.}

View:

01.<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<List<MvcApplication1.Models.EmployeeInfo>>" %>
02. 
03.<!DOCTYPE html>
04.<html>
05.<head runat="server">
06.    <title>Index</title>
07.    <style type="text/css">
08.        .cell
09.        {
10.            width: 75px;
11.            padding: 5px 5px 5px 5px;
12.            float: left;
13.            border: 1px solid #CCCCCC;
14.        }
15.    </style>
16.</head>
17.<body>
18.    <div>
19.        <% foreach (MvcApplication1.Models.EmployeeInfo oEmp in Model)
20.           { %>
21.        <div style="clear: both">
22.            <div class="cell">
23.                <%= oEmp.Empno %>
24.            </div>
25.            <div class="cell">
26.                <%= oEmp.Ename %>
27.            </div>
28.        </div>
29.        <%} %>
30.    </div>
31.</body>
32.</html>


Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following

Video/Screen Recording (may not have audio narration/annotations)
You need to Login or Join for FREE to download the following