[英]Difficulty listing data using a view model in ASP.NET MVC
I am trying to pass data from a view model which derives from 2 models. 我正在尝试从2个模型派生的视图模型传递数据。 The controller looks like this
控制器看起来像这样
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);
}
It is coming up with errors saying the name "a" does not exist in the current context? 出现错误,说在当前上下文中不存在名称“ a”? Any ideas as to what is going on?
有什么想法吗?
I am really new to this, I followed these instructions using website http://techfunda.com/howto/262/list-data-using-viewmodel which does what I want SessionDetails
to do. 我真的很
SessionDetails
,我使用http://techfunda.com/howto/262/list-data-using-viewmodel网站遵循了这些说明,该网站可以执行SessionDetails
要做的事情。
I already have a view for this. 我已经对此有看法。
UPDATE UPDATE
I have made following changes: 我进行了以下更改:
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);
}
But when i run it, i get this: an exception of type System.NotSupportedException
occurred in EntityFramework.SqlServer.dll but was not handled in user code. 但是当我运行它时,我得到了: EntityFramework.SqlServer.dll中发生了
System.NotSupportedException
类型的异常,但未在用户代码中处理。
Additional information: the entity or complex type fypag.Models.SessionViewModel
cannot be constructed in a LINQ to Entities query. 附加信息:实体或复杂类型
fypag.Models.SessionViewModel
不能在LINQ to Entities查询中构造。
I think your model should look like this. 我认为您的模型应如下所示。
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,
});
UPDATE: 更新:
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.