Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-30
3
Medium Priority
?
334 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 9

Accepted Solution

by:
richard_hughes earned 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

916 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