我有以下两个数据库表,它们是这样建模的:
CalloutToProduct
-----------------
| id
| CalloutID
| CalloutTypeID
| ProductIdentifier
| DateStart
| DateEnd
| IsActive
-----------------
CalloutTypeValue
-----------------
| id
| CalloutTypeID
| Value
| Comment
-----------------(顺便说一句,数据库的建模似乎非常糟糕,这些表(如果不是所有数据库的话)都是标准化的,也没有与在这些表上设置适当的PK或FK的适当关系,所以在所有条件相同的情况下,我认为这种关系是1比1。
作为MVC的新手,我不确定这是否是我的问题的一部分。)
继续,我还以代码第一的方式创建了这两个类模型:
public class CalloutToProduct
{
[Key, ForeignKey("CalloutTypeValues")]
public int id { get; set; }
public int CalloutID { get; set; }
public int? CalloutTypeID { get; set; }
public string ProductIdentifier { get; set; }
public DateTime? DateStart { get; set; }
public DateTime? DateEnd { get; set; }
public int? IsActive { get; set; }
public virtual CalloutTypeValue CalloutTypeValues { get; set; }
}
public class CalloutTypeValue
{
[Key, ForeignKey("CalloutToProduct")]
public int id { get; set; }
public int? CalloutTypeID { get; set; }
public string Value { get; set; }
public string Comment { get; set; }
public virtual CalloutToProduct CalloutToProduct { get; set; }
}
And a context model:
public class CalloutToProduct
public class CalloutContext : DbContext
{
public DbSet<CalloutToProduct> CalloutToProduct { get; set; }
public DbSet<CalloutTypeValue> CalloutTypeValue { get; set; }
public DbSet<CalloutValue> CalloutValue { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}我还有一个使用DbExtensions.Include方法的控制器,设置如下:
public ViewResult Index(int page = 1)
{
var callouts = db.CalloutToProduct.Include(c => c.CalloutTypeValues);
return View(callouts
.OrderBy(p => p.DateStart)
.Skip((page - 1) * PageSize)
.Take(PageSize));
}一幅这样的景象:
@model IEnumerable<MarketingWebsiteTools.Models.CalloutToProduct>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
CalloutID
</th>
<th>
CalloutTypeID
</th>
<th>
ProductIdentifier
</th>
<th>
@Html.ActionLink("Date Start", "Index", new { SortOrder=ViewBag.NameSortParm })
</th>
<th>
DateEnd
</th>
<th>
IsActive
</th>
<th>Value</th>
<th>Comments</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.CalloutID)
</td>
<td>
@Html.DisplayFor(modelItem => item.CalloutTypeID)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductIdentifier)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateStart)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateEnd)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsActive)
</td>
<td>
@Html.DisplayFor(modelItem => item.CalloutTypeValues.Value)
</td>
<td>
@Html.DisplayFor(modelItem => item.CalloutTypeValues.Comment)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.id }) |
@Html.ActionLink("Details", "Details", new { id=item.id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.id })
</td>
</tr>
}
</table>我的问题是item.CalloutTypeValues对象是空的,所以在我的视图中没有填充值和注释。
有人能告诉我我做错了什么吗?
我在Googled上搜索了一下,有人提到我需要使用虚拟关键字来解释加载这些实体,但是我相信这就是我在实体类中所做的。
增添:
我使用LINQ修改了Controller,如下所示:
public ViewResult Index(int page = 1){
var callouts = from c in db.CalloutToProduct
join ctv in db.CalloutTypeValue on c.CalloutTypeID equals ctv.CalloutTypeID
select new
{
c.id,
c.CalloutID,
c.CalloutTypeID,
c.ProductIdentifier,
c.DateStart,
c.DateEnd,
c.IsActive,
ctv.Value,
ctv.Comment
};
return View(callouts
.OrderBy(p => p.DateStart)
.Skip((page - 1) * PageSize)
.Take(PageSize));
}但是我认为模型不正确,所以我收到了以下错误:
传递到字典中的模型项为'System.Data.Entity.Infrastructure.DbQuery1[<>f__AnonymousType19[System.Int32,System.Int32、System.Nullable1[System.Int32],System.String,System.Nullable1System.DateTime,System.Nullable1[System.DateTime],System.Nullable1System.Int32、System.String、System.String]‘,但本词典需要一个'System.Collections.Generic.IEnumerable`1MarketingWebsiteTools.Models.CalloutToProduct'.类型的模型项
提前谢谢。
道格
发布于 2012-03-26 16:55:05
看上去你的视图
@model IEnumerable<MarketingWebsiteTools.Models.CalloutToProduct>你给它传递了一个匿名类型。尝试创建一个名为ViewModel的CalloutToProductViewModel,并将查询项映射到ViewModel
附带注意:让你的控制器精力充沛,把你的业务放在一项服务中。
视图
@model IEnumerable<MarketingWebsiteTools.Models.CalloutToProductViewModel>控制器
public ViewResult Index(int page = 1){
var model = CalloutService.GetCallouts(page);
return View(model);
}服务
public class CalloutService
{
public CalloutToProductViewModel GetCallouts(int page)
{
var callouts = from c in db.CalloutToProduct
join ctv in db.CalloutTypeValue on c.CalloutTypeID equals ctv.CalloutTypeID
select new
{
c.id,
c.CalloutID,
c.CalloutTypeID,
c.ProductIdentifier,
c.DateStart,
c.DateEnd,
c.IsActive,
ctv.Value,
ctv.Comment
};
// ******
var calloutVM = // map the anonymous type to a POCO View Model and do your orders
// ******
// .OrderBy(p => p.DateStart)
// .Skip((page - 1) * PageSize)
// .Take(PageSize));
return (calloutsVM)
}
}https://stackoverflow.com/questions/9875554
复制相似问题