3

这里是编程环境。

  • 框架:ASP.NET 框架 4
  • 语言:Visual C# 2010
  • 设计模式:MVC 4
  • 查看引擎:剃刀

这是场景。

我正在使用以下代码创建一个网页:

项目跟踪.cs

using System;

namespace MyProject.Models
{
    public class ProjectTracking
    {
        public int ProjectID
        {
            get;
            set;
        }
        public string ProjectName
        {
            get;
            set;
        }
        public DateTime ProjectDate
        {
            get;
            set;
        }
    }
}

项目跟踪控制器.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...
        }
    }
}

索引.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>
}

到目前为止,这似乎有效。这是我面临的挑战。我想将数组中的结果插入到 ProjectID 中。我需要做些什么才能做到这一点?

例如,

        int[] projectInt = new int[3] { 1, 2, 3 };
        public ActionResult Index()
        {
            ProjectTracking[] projects =
            {
                new ProjectTracking
                {
                    ProjectID = projectInt???...

也许这是一种不好的方法,并且需要完全不同的策略?

4

2 回答 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);
    }
于 2013-08-29T00:17:13.947 回答
1

你可以尝试类似的东西

    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)
    }
于 2013-08-29T01:37:23.650 回答