如何从控制器返回多维列表?

时间:2014-08-11 05:30:58

标签: c# asp.net-mvc asp.net-mvc-4 model-view

我正在尝试从控制器返回多维列表以查看。 我有一个

  • 型号
  • ViewModel
  • 查看
  • 控制器

视图模型:

 public class HomeViewModel
    {
        public DateTime Date { get; set; }
        public List<Snapshot> SnapshotItems { get; set; }
    }

查看:

@model IEnumerable<myproject.ViewModels.HomeViewModel>
<div class="pi-section pi-padding-top-20 pi-padding-bottom-10">
    @foreach (var items in Model)
    {
        <div class="pi-container">
            <div class="pi-col-lg-12">
                @items.Date.ToString("MMM, dd, 2014")
            </div>
        </div>
        <div class="pi-container">
            @foreach (var item in items.SnapshotItems)
            {
                <div class="item pi-col-lg-4">
                    <div class="">@Html.ActionLink(item.BrandName, "Index", "Brand", new { guid = item.BrandGuid, brandName = FriendlyUrl.Convert((item.BrandName).ToString()) }, null)</div>
                    <div class="snapshot-photo-home">
                        <a class="image_rollover_bottom con_borderImage" data-description="ZOOM IN" href="@Url.Action("html", "snapshot" , new { guid = item.Guid })" target="_blank">
                            <img class="lazyload" src="@item.Thumbnail" width="300" />
                        </a>
                    </div>
                    <span class="home-subject">@item.Subject</span>
                    <div class="snapshot-action">
                    </div>
                </div>
            }
        </div>
    }
</div>

控制器:

public PartialViewResult RecentEmails()
        {
            int days = 2;
            HomeViewModel viewModel = new HomeViewModel();
            List<Snapshot> snapshots = new List<Snapshot>();
            List<HomeViewModel> viewModelList = new List<HomeViewModel>();

            for (int count = 0; count < days; count++)
            {

                viewModel.Date = DateTime.Now.AddDays(count * (-1));

                snapshots = GetEmailSnapshot(9, viewModel.Date);

                viewModel.SnapshotItems = snapshots;
            }
            viewModelList.Add(viewModel);
            return PartialView(viewModelList.AsEnumerable());
        }

private List<Snapshot> GetEmailSnapshot(int numSnapshots, DateTime dateReceived)
        {
            List<Snapshot> snapshots = new List<Snapshot>();

            string dbConnString = WebConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
            MySqlConnection dbConn = new MySqlConnection(dbConnString);
            dbConn.Open();
            MySqlCommand dbCmd = new MySqlCommand();

            string date = dateReceived.ToString("yyyy/MM/dd");

            dbCmd.CommandText = "SELECT a.snapshot_id, a.subject, a.snapshot_guid, a.date_sent, a.image, a.thumbnail, a.status, b.name, b.brand_guid FROM snapshots a INNER JOIN brands b ON a.brand_id = b.brand_id WHERE status = 1 AND a.archive = 0 AND (thumbnail IS NOT NULL OR thumbnail = '') AND DATE(date_sent) = '" + date + "' GROUP BY a.brand_id ORDER BY date_sent DESC LIMIT " + numSnapshots;
            dbCmd.Connection = dbConn;
            MySqlDataReader dbResult = dbCmd.ExecuteReader();
            while (dbResult.Read())
            {
                snapshots.Add(new Snapshot
                {
                    SnapshotId = Convert.ToInt32(dbResult["snapshot_id"]),
                    Guid = dbResult["snapshot_guid"].ToString().Replace("-", String.Empty),
                    BrandGuid = dbResult["brand_guid"].ToString(),
                    BrandName = HttpUtility.HtmlDecode(dbResult["name"].ToString()),
                    Subject = MyHelpers.Ellipsis(HttpUtility.HtmlDecode(dbResult["subject"].ToString()), 100),
                    DateTimeSent = Convert.ToDateTime(dbResult["date_sent"]),
                    Image = dbResult["image"].ToString(),
                    Status = Convert.ToInt32(dbResult["status"]),
                    Thumbnail = dbResult["thumbnail"].ToString()
                });
            }
            dbResult.Close();

            dbCmd.Dispose();
            dbConn.Close();

            return snapshots;
        }

我遇到的问题是在VIEW上。它从GetEmailSnapshot返回相同的数据集。因此,我以2014年8月8日的SAME 9记录为例。

是否有理由不将8/10/2014添加到列表中,然后在列表中添加8/9/2014?

1 个答案:

答案 0 :(得分:0)

我想我可能过于复杂了。这是简化版:FIXED

public PartialViewResult RecentEmails()
        {
            int days = 2;

            List<HomeViewModel> viewModelList = new List<HomeViewModel>();

            for (int count = 0; count < days; count++)
            {
                DateTime date = DateTime.Now.AddDays(count * (-1));
                viewModelList.Add(new HomeViewModel
                {
                    Date = date,
                    SnapshotItems = GetEmailSnapshot(9, date)

                });
            }

            return PartialView(viewModelList.AsEnumerable());
        }