Solved

Creating HTML Table for ASP.NET MVC and Adding data to it

Posted on 2011-09-30
3
308 Views
Last Modified: 2012-06-27
Hi

Can you please suggest me how to create HTML Table and Assign the data to it by looping.
0
Comment
Question by:praveen1981
3 Comments
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36890289
0
 
LVL 9

Accepted Solution

by:
richard_hughes earned 500 total points
ID: 36890634
Hello praveen1981

In MVC 2 in C# 3.5, try the attached code:

You can see that there is a model called TableData. In the controller, this model is created as a list and then passed into the 'Table' view. Inside the view, we loop through each of the TableData items and then render them into the HTML.

Notice though, the first line in the view:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IList<MvcApplication1.Models.TableData>>" %>

The view inherits an IList<MvcApplication1.Models.TableData> This is important as we want a list of TableData, rather than just a single item.

Thanks,

Richard Hughes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
	[HandleError]
	public class HomeController : Controller
	{
		public ActionResult Index()
		{
			ViewData["Message"] = "Welcome to ASP.NET MVC!";

			return View();
		}

		public ActionResult About()
		{
			return View();
		}

		public ActionResult Table()
		{
			var rows = new List<TableData>()
			{
				new TableData()
				{
					Id = 1,
					Name = "Bob Jones",
					IsActive = true,
				},
				new TableData()
				{
					Id = 2,
					Name = "John Barker",
					IsActive = false,
				},
				new TableData()
				{
					Id = 3,
					Name = "Jane Smith",
					IsActive = true,
				},
				new TableData()
				{
					Id = 4,
					Name = "Barry Wattson",
					IsActive = true,
				},
			};

			return this.View(rows);
		}
	}
}

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication1.Models
{
	public class TableData
	{
		public int Id { get; set; }
		public string Name { get; set; }
		public bool IsActive { get; set; }
	}
}

Open in new window

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IList<MvcApplication1.Models.TableData>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	ViewPage1
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <table>
		<thead>
			<tr>
				<th>ID</th>
				<th>Name</th>
				<th>Is Active</th>
			</tr>
		</thead>
		<% foreach (var row in this.Model)
		   { %>
		   <tr>
				<td><%: row.Id %></td>
				<td><%: row.Name %></td>
				<td><%: row.IsActive %></td>
		   </tr>
		<% } %>
	</table>

</asp:Content>

Open in new window

0
 

Author Closing Comment

by:praveen1981
ID: 36915811
yes, it solved
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now