0
我有我使用我的基础模型制作的ViewModel。我也能够将数据插入表中。从MVC中的多个表中检索数据ASP.NET
我想现在检索数据并显示在我的表单上。我应该在我的索引视图中写什么代码。
我的视图模型从2个不同的基本模型组成的属性.Hence,在视图中,当我试图从我的ViewModel获得使用属性信息,它给出了“对象不设置到对象的实例”错误。
代码如下:
ViewModel类:
public class VideoContextViewModel
{
public string VideoName { get; set; }
public string DescriptionVideo { get; set; }
public string actor { get; set; }
public HttpPostedFileBase references { get; set; }
}
基本模型类:
public class video
{
public int ID { get; set; }
public string VideoName { get; set; }
public string DescriptionVideo { get; set; }
public string FilmName { get; set; }
public int ratings { get; set; }
}
public class videoDetails
{
public int VideoDetailsID { get; set; }
public string actor { get; set; }
public byte[] reference { get; set; }
public int ID { get; set; }
public virtual video Video { get; set; }
}
索引视图:
@model IEnumerable< ConnectDatabase. VideoContextViewModel>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.VideoName)
</td>
<td>
@Html.DisplayFor(modelItem => item.DescriptionVideo)
</td>
<td>
@Html.DisplayFor(modelItem => item.actor)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ })
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
当我运行的解决方案,我得到的错误:
Object cannot be set to an instance of an object. I hover over the "Model" in the foreach loop and it shows as Null.
因此,我应该写在这里,以便从两个表显示数据?
后一些代码请。 – 2013-02-28 09:53:37
显示您的代码... – 2013-02-28 09:53:39