使用ASP.NET MVC中的视图模型列出数据困难

时间:2017-03-22 20:45:28

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

我正在尝试从2个模型派生的视图模型中传递数据。控制器看起来像这样

public ActionResult SessionDetails()
{
    var Sessions = (from a in db.Appointments
                    join c in db.Clients
                    on a.clientid equals c.id into SessionList
                    from c in SessionList.DefaultIfEmpty()
                    select new SessionViewModel()
                    {

                        id = a.id,
                        sessionnotes = a.sessionnotes,
                        firstname = c.firstname,
                        date = a.date,

                    }).ToList()
             .Select(x => new SessionViewModel()
             {

                 id = a.id,
                 sessionnotes = a.sessionnotes,
                 firstname = c.firstname,
                 date = a.date,
             });

    return View(Sessions);
}

出现错误,说当前上下文中不存在名称“a”?关于发生了什么的任何想法?

我对此非常陌生,我使用网站http://techfunda.com/howto/262/list-data-using-viewmodel按照这些说明操作,该网站执行我想要SessionDetails做的事情。

我已经有了这个观点。

更新

我做了以下更改:

public ActionResult SessionDetails()
{
    var Sessions = (from a in db.Appointments
                    join c in db.Clients
                    on a.clientid equals c.id into SessionList
                    from c in SessionList.DefaultIfEmpty()
                    select new SessionViewModel()
                    {

                        id = a.id,
                        sessionnotes = a.sessionnotes,
                        firstname = c.firstname,
                        date = a.date,

                    }).ToList();


    return View(Sessions);
}

但是当我运行它时,我得到了这个: EntityFramework.SqlServer.dll 中出现类型System.NotSupportedException的例外但未在用户代码中处理。

其他信息:无法在LINQ to Entities查询中构造实体或复杂类型fypag.Models.SessionViewModel

1 个答案:

答案 0 :(得分:0)

我认为你的模型应该是这样的。

   var Sessions = (from a in db.Appointments
                             join c in db.Clients
                             on a.clientid equals c.id into SessionList
                             from c in SessionList.DefaultIfEmpty()
                             select new SessionViewModel()
                             {

                                id = a.id,
                                 sessionnotes = a.sessionnotes,
                                 firstname = c.firstname,
                                 date = a.date,

                             }).ToList()
                   .Select(x => new SessionViewModel()
                    {
                         id = x.id,
                         sessionnotes = x.sessionnotes,
                         firstname = x.firstname,
                         date = x.date,
                    });

<强>更新

var Sessions = (from a in db.Appointments
                join c in db.Clients
                on a.clientid equals c.id into SessionList
                from c in SessionList.DefaultIfEmpty()
                .Select(y => new SessionViewModel()
                {
                    id = y.id,
                    sessionnotes = y.sessionnotes,
                    firstname = y.firstname,
                    date = y.date,

                }).ToList();