Solved

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

Posted on 2011-09-30
3
313 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 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