我的模型是null

时间:2016-02-02 06:30:38

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

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

我的控制器

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

我的模特

{{1}}

2 个答案:

答案 0 :(得分:0)

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

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

如果开销不重要且数据很小,只需发送db.Announcer

答案 1 :(得分:0)

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

}