Here is the programming environment.
- Framework: ASP.NET Framework 4
- Language: Visual C# 2010
- Design Pattern: MVC 4
- View Engine: Razor
Here is the scenario.
I'm creating a web page with the following codes:
ProjectTracking.cs
using System;
namespace MyProject.Models
{
public class ProjectTracking
{
public int ProjectID
{
get;
set;
}
public string ProjectName
{
get;
set;
}
public DateTime ProjectDate
{
get;
set;
}
}
}
ProjectTrackingController.cs
using ProjectTracking.Models;
using System;
using System.Web.Mvc;
namespsace MyProject.Controllers
{
public class ProjectTrackingController:Controller
{
public ActionResult Index()
{
ProjectTracking[] projects =
{
new ProjectTracking
{
ProjectID = 1,
ProjectName = "Project 1",
ProjectDate = DateTime.Now
},
new ProjectTracking
{
ProjectID = 2,
ProjectName = "Project 2",
ProjectDate = DateTime.Now.AddDays(1)
},
new ProjectTracking
{
ProjectID = 3,
ProjectName = "Project 3",
ProjectDate = DateTime.Now.AddDays(2)
}
};
}
public ActionResult New()
{
...omitted for brevity...
}
}
}
Index.cshtml
@using MyProject.Models
@model ProjectTracking[]
@{
ViewBag.Title = "Project Tracking";
}
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>@ViewBag.Message</h2>
</hgroup>
<p>
@Html.ActionLink("Add a New Project", "New")
</p>
@if (Model.Length > 0)
{
<table>
<thead>
<tr>
<th>
ID
</th>
<th>
Name
</th>
<th>
Date
</th>
</tr>
</thead>
<tbody>
@foreach (ProjectTracking p in Model
{
<tr>
<td>
@p.ProjectID
</td>
<td>
@p.ProjectName
</td>
<td>
@p.ProjectDate
</td>
</tr>
}
</tbody>
</table>
}
else
{
<h2>No project tracking data found.</h2>
}
This seems to work so far. Here is the challenge I'm facing. I'd like to insert results from an array to ProjectID instead. What would I need to do to make this happen?
For example,
int[] projectInt = new int[3] { 1, 2, 3 };
public ActionResult Index()
{
ProjectTracking[] projects =
{
new ProjectTracking
{
ProjectID = projectInt???...
Maybe this is a bad approach and would require a different strategy altogether?