3

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?

2 Answers 2

2
    int[] projectInt = new int[3] { 1, 2, 3 };

    List<ProjectTracking> projects = new List<ProjectTracking>();
    foreach (var i in projectInt)
    {
      var project = new ProjectTracking{
                            ProjectID = projectInt[i],
                            ...
      }
      projects.Add(project);
    }
Sign up to request clarification or add additional context in comments.

3 Comments

I get the following error message when I tried that: The model item passed into the dictionary is of type 'System.Collections.Generic.List` 1[MyProject.Models.ProjectTracking]', but this dictionary requires a model item of type 'MyProject.Models.ProjectTracking[]'.
Sorry, instead of using List<ProjectTracking> projects, use ProjectTracking[] projects
Oh, never mind! I revised the View to use @model IEnumerable<MyProject.Models.ProjectTracking> and it's working fine and dandy.
1

You can try something like

    public ActionResult Index()
    {
       int[] projectInt = new int[3]{1, 2, 3};
       List<ProjectTracking> projects = new List<ProjectTracking>();
       for(var i in projectInts)
       {
            projects.Add(
            new ProjectTracking
            {
                ProjectID = projectInt[i],
                ProjectName = "Project " + projectInt[i],
                ProjectDate = DateTime.Add(projectInt[i])
            });
        }
       return View(projects)
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.