C# 从另一个数据库asp.net mvc中的另一个模型引用一个模型
我的控制器中有以下方法:C# 从另一个数据库asp.net mvc中的另一个模型引用一个模型,c#,asp.net-mvc,C#,Asp.net Mvc,我的控制器中有以下方法: public ActionResult OwnerList() { var owners = (from s in db.Owners orderby s.Post.PostName select s).ToList(); var v
public ActionResult OwnerList()
{
var owners = (from s in db.Owners
orderby s.Post.PostName
select s).ToList();
var viewModel = owners.Select(t => new OwnerListViewModel
{
Created = t.Created,
PostName = t.Post.PostName,
Dormant = t.Dormant,
OwnerId = t.OwnerId,
});
return PartialView("_OwnerList", viewModel);
}
运行此命令时,我从owners变量中得到以下错误:
{“无效的对象名'dbo.Post'。”
我的模型就是这些
在所有者数据库中
public class Owner
{
public int OwnerId { get; set; }
[Column(TypeName = "int")]
public int PostId { get; set; }
[Column(TypeName = "bit")]
public bool Dormant { get; set; }
[Column(TypeName = "datetime2")]
public DateTime Created { get; set; }
public virtual ICollection<Asset> Assets { get; set; }
public virtual Post Post { get; set; }
}
公共类所有者
{
public int OwnerId{get;set;}
[列(TypeName=“int”)]
公共int PostId{get;set;}
[列(TypeName=“bit”)]
公共布尔休眠{get;set;}
[列(TypeName=“datetime2”)]
已创建公共日期时间{get;set;}
公共虚拟ICollection资产{get;set;}
公共虚拟Post{get;set;}
}
在人民数据库中
public class Post
{
public int PostId { get; set; }
[StringLength(50)]
[Column(TypeName = "nvarchar")]
public string PostName { get; set; }
[Column(TypeName = "bit")]
public bool Dormant { get; set; }
[StringLength(350)]
[Column(TypeName = "nvarchar")]
public string Description { get; set; }
public virtual ICollection<Contract> Contracts { get; set; }
public virtual ICollection<Owner> Owners { get; set; }
}
公共类职位
{
公共int PostId{get;set;}
[长度(50)]
[列(TypeName=“nvarchar”)]
公共字符串PostName{get;set;}
[列(TypeName=“bit”)]
公共布尔休眠{get;set;}
[长度(350)]
[列(TypeName=“nvarchar”)]
公共字符串说明{get;set;}
公共虚拟ICollection协定{get;set;}
公共虚拟ICollection所有者{get;set;}
}
意见是:
@model IEnumerable<IAR.ViewModels.OwnerListViewModel>
<table class="table">
@foreach (var group in Model
.OrderBy(x => x.Dormant)
.GroupBy(x => x.Dormant))
{
<tr class="group-header">
@if (group.Key == true)
{
<th colspan="12"><h3>Dormant:- @group.Count()</h3></th>
}
else
{
<th colspan="12"><h3>Active:- @group.Count()</h3></th>
}
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.PostName)
</th>
<th>
@Html.DisplayNameFor(model => model.Created)
</th>
<th></th>
</tr>
foreach (var item in group
)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.PostName) @Html.HiddenFor(modelItem => item.OwnerId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Created)
</td>
<td>
@if (group.Key == true)
{
@Html.ActionLink("Make Active", "Dormant", new { ownerId = item.OwnerId })
}
else
{
@Html.ActionLink("Make Dormant", "Dormant", new { ownerId = item.OwnerId })
}
</td>
</tr>
}
}
</table>
@model IEnumerable
@foreach(模型中的var组
.OrderBy(x=>x.Dormant)
.GroupBy(x=>x.dorment))
{
@if(group.Key==true)
{
休眠:-@group.Count()
}
其他的
{
活动:-@group.Count()
}
@DisplayNameFor(model=>model.PostName)
@DisplayNameFor(model=>model.Created)
foreach(组中的var项目
)
{
@Html.DisplayFor(modeleItem=>item.PostName)@Html.HiddenFor(modeleItem=>item.OwnerId)
@DisplayFor(modelItem=>item.Created)
@if(group.Key==true)
{
@ActionLink(“激活”、“休眠”,新的{ownerId=item.ownerId})
}
其他的
{
@ActionLink(“使休眠”,“休眠”,新的{ownerId=item.ownerId})
}
}
}
我确信这很简单,但我没有正确地做什么来允许它引用所有者的帖子呢?给定
s.Post
为null
,您禁用了实体框架的延迟加载。启用延迟加载或显式加载导航属性:
from s in db.Owners.Include(o => o.Post)
orderby s.Post.PostName
...
至于您的编辑,如果
Owner.Post
位于不同的数据库中,则这是一个完全不同的问题。在web上搜索该信息,请参见示例和以获取一些提示。要么在数据库级别链接它们(使用链接服务器或同义词和视图),要么在代码中修复它们(在所有者
上循环,并查找他们的帖子
)用HTTP Post decoration装饰方法正确地说,如何填充db.Owners
?您的推荐信(“允许其引用所有者的帖子”)是正确的,但是Post没有被填充。忘记视图-它在到达视图之前就已经崩溃了。intellisense中存在以下错误,无法将lambda表达式转换为类型“string”,因为它不是委托类型,“如果您研究该错误,您会发现您需要使用System.Data.Entity使用代码>指令导入该扩展名方法。谢谢,但是列表视图中的PostName为空,这是另一个问题。你确定这个所有者有一个PostID集吗?查看了所有者表后,save方法添加了一个自动生成的PostID编号,而不是所选的实际PostID,尽管我不知道为什么