对于强类型视图,如何为模型数据动态添加数组值?

时间:2013-08-29 00:00:02

标签: asp.net-mvc-4

这是编程环境。

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

以下是该方案。

我正在创建一个包含以下代码的网页:

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>
}

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

例如,

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

也许这是一种糟糕的方法,需要一个完全不同的策略?

2 个答案:

答案 0 :(得分: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);
    }

答案 1 :(得分: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)
    }