【发布时间】:2015-12-31 03:33:10
【问题描述】:
我正在尝试使用视图发送数据并将其打印出来,但由于我对 C# 非常陌生,因此我非常努力。
这是我的模型 ViewModel:
namespace P104.Models
{
public class ViewModel
{
}
public class Location
{
public int loc_id { get; set; }
public string loc_name { get; set; }
}
public class Competentie
{
public int Comp_id { get; set; }
public string competentie { get; set; }
}
public class MyViewModel
{
public User User { get; set; }
public IEnumerable<Locations> Locations { get; set; }
public IEnumerable<Competenties> Competenties { get; set; }
}
}
这是我在控制器中的功能
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
User user = db.user.Find(id);
if (user == null)
{
return HttpNotFound();
}
var competenties =
from usercomp in dbE.UserComp
join comp in dbE.Competenties on usercomp.fk_Comp_id equals comp.comp_id
where usercomp.fk_user_id == id
select new Competenties { competentie = comp.competentie };
var locations =
from userloc in dbE.UserLoc
join loc in dbE.Locations on userloc.fk_location_id equals loc.loc_id
where userloc.fk_user_id == id
select new Locations { loc_name = loc.loc_name };
var model = new MyViewModel
{
User = user,
Locations = locations.ToList(), // eagerly fetch the data that will be needed in the view
Competenties = competenties.ToList(), // eagerly fetch the data that will be needed in the view
};
return View(model);
}
他就是我尝试在视图中打印出来的方式:
@foreach (var location in Model.Locations)
{
<dt>@location.locname</dt>
<dd>@location.locname</dd>
}
@foreach (var competentie in Model.Competenties)
{
<dt>@competentie.competentie</dt>
<dd>@competentie.competentie</dd>
}
我总是收到此错误
实体或复杂类型“P104.Models.Locations”不能 在 LINQ to Entities 查询中构造。
我找到了一些解决方案,但我很难将它们应用到我的代码中,因此它们不起作用。 提前感谢您的帮助
【问题讨论】:
标签: c# asp.net-mvc entity-framework linq