Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

772 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