除了 ICollection 之外,我的视图中显示的所有其他内容。
测试模型
public class Tes
{
[Key]
public int tID { get; set; }
public int FromUserID { get; set; }//user logged in
public int ToUserId { get; set; } //user to be sent
public int itemTId { get; set; } //single item
public virtual ICollection<Item> items { get; set; }
}
项目型号
public class Item
{
public int UserId { get; set; }
public int ID { get; set; }
public string item_name { get; set; }
public string item_description { get; set; }
public string item_code { get; set; }
public DateTime dateAdded { get; set; }
public int catId { get; set; }
public int? isSelected { get; set; }
public int isQuick { get; set; }
public virtual ICollection<Tes> tes{ get; set; }
}
public class ItemDBContext : DbContext
{
public ItemDBContext()
: base("name=ItemDbContext")
{ }
public DbSet <Item> Items { get; set; }
public DbSet<Category> Cats { get; set; }
public DbSet<Tes> Tes{ get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Tes>().HasMany(e => e.items).WithMany(e => e.tes);
}
}
控制器
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Tes take)
{
if (ModelState.IsValid)
{
db.Trades.Add(take);
//do I add ICollection seperately?
db.SaveChanges();
return RedirectToAction("Index");
}
return View(take);
}
看法
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.tID)
</td>
<td>
@Html.DisplayFor(modelItem => item.ToUserId)
</td>
<td>
@Html.DisplayFor(modelItem => item.itemTId)
</td>
<td>
@Html.DisplayFor(modelItem => item.items)//not displaying?
</td>
阿贾克斯邮政
var take= {
FromUserID: uid,
ToUserID: TradeeUid,
itemTradedForId: TradeeItemId,
items: ids,
__RequestVerificationToken: $("[name='__RequestVerificationToken']").val()
}
//jQuery.ajaxSettings.traditional = true
$.ajax({
type: "POST",
url: "/Trade/Create",
data: take
//dataType:JSON
//contentType: "application/json; charset=utf-8",
//data: JSON.stringify(take)
});
这就是我得到的。
如果我尝试items
从嵌套的 for 循环中获取。我收到一个错误
修复了:
但仍然没有ICollection
显示。难道它们没有被存储?