无法在视图ASP.NET MVC中显示DB表数据

本文关键字:显示 DB 数据 MVC NET 视图 ASP | 更新日期: 2023-09-27 17:51:06

通过将表从数据库资源管理器拖放到O/R设计器中,创建了一个DataContext。当我在控制器中尝试以下代码时,在我的视图中没有显示任何内容

Table<week> weektab = context.GetTable<week>();
var report = from w in weektab select new myModel(){col1, col2...};
return PartialView(report);

我的Partialview是强类型的,我在foreach循环中显示Model的字段。该代码适用于其他表,但不显示Week表的任何内容。我错过了什么?


编辑1:我验证了Week表有110行。
第2版:
这是代码

Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace proj.Models
{
    public class WeeklyReportModel
    {
    public string name { get; set; }
    public string mon { get; set; }
    public string tue { get; set; }
    public string wed { get; set; }
    public string thu { get; set; }
    public string fri { get; set; }
    public string sat { get; set; }
}

}
Controller snippet

public PartialViewResult showResult(string unit, string day)
    {
        this.context = new projDataContext(ConfigurationManager.ConnectionStrings["constr"].ToString());
        ...
        Table<week> weektab = context.GetTable<week>();
         ...
        var report = from sat1 in weektab
                            select new WeeklyReportModel{ name = sat1.col1};
        return PartialView(report);
    }<br/>

PartialView

@using proj.Models
@model IEnumerable<WeeklyReportModel>
<table>
    ...
            @foreach (var p in Model)
            {
                <tr>
                    <td>@p.name</td>
                    <td>@p.mon</td>
                      <td>@p.tue</td>
                      <td>@p.wed</td>
                      <td>@p.thu</td>
                      <td>@p.fri</td>
                      <td>@p.sat</td>
                </tr>
            }
</table>

无法在视图ASP.NET MVC中显示DB表数据

应OP的要求,我将这个答案作为结论发布到上面的评论线索中。

我只能认为周表的定义有问题。有代表周表的班级吗?我只是在看这个问题,它似乎与您的相似