我的模型为空

本文关键字:模型 我的 | 更新日期: 2023-09-27 18:32:53

为什么模型为空?我认为我的方法错误,它没有将数据传递到视图

我的控制器

public ActionResult AnnounceRead()
{
    using (var db = new HarmonyMainServerDbContext())
    {
        var announceList = from r in db.Announcer
                           select new { r.AnnounceTitle, r.AnnounceLink, };
        return View("Index", announceList);
    }
}

我的观点

@model IEnumerable<Harmony.Models.ANNOUNCEMENT>
<ul>
    @if (Model != null) 
    {
        foreach (var item in Model)           
        {
            <li><a href="@item.AnnounceLink"> Title: @item.AnnounceTitle</a></li>                 
        }        
    }                
</ul>

我的模型

public class ANNOUNCEMENT
{
    [Key]
    public int AnnounceID { get; set; }
    public string AnnounceTitle { get; set; }
    public string AnnounceRemarks { get; set; }
    public string AnnounceLink { get; set; }
    public int CreatedBy { get; set; }
}

我的模型为空

public ActionResult AnnounceRead()
 {
    using (var db = new HarmonyMainServerDbContext())
{
    var announceList = db.Announcer.Select(x=> new ANNOUNCEMENT{
                       AnnounceTitle = x.AnnounceTitle,
                       AnnounceLink = x.AnnounceLink
 }).ToList();
    return View("Index", announceList);
}

}

var announceList = (from r in db.Announcer
                                   select new { r.AnnounceTitle, r.AnnounceLink }).ToList();

编辑:只需添加.ToList();

如果开销不重要且数据很小,只需发送数据库即可。播音员