从MVC ASP.NET中的多个表中检索数据



我有一个ViewModel,我使用我的基本模型。我还能够向表中插入数据。

我现在想检索数据并显示在我的表单上。我应该写什么代码在我的索引视图

我的ViewModel是由2个不同的基本模型的属性组成的。因此,在视图中,当我试图从我的ViewModel属性获取信息时,它给出了"对象不设置为对象的实例"错误。


代码如下:

ViewModel Class:

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不能设置为对象的实例。我将鼠标悬停在foreach循环中的"Model"上,它显示为Null。

因此,我应该在这里写些什么来显示两个表中的数据?

下面是一个示例,您可以尝试这种方法:

 public class Equipment
    {
       [Key] public int EquipID { get; set; }
        public string EquipName { get; set; }
        public string EquipDescription { get; set; }
        public string EquipSerialNumber { get; set; }
        public string EquipImage { get; set; }
        public string EquipManufacturor { get; set; }
        public string EquipLocation { get; set; }
        public string EquipCalibrationFreq { get; set; }
        public virtual ICollection<Calibration> Calibrations { get; set; }
    }
 public class Calibration
    {
      [Key]  public int RecID { get; set; }
       public int EquipID { get; set; }
        public DateTime CalibrationDate { get; set; }
        public decimal? CalibrationCost { get; set; }
        public int ContactID { get; set; } 
        public String CalibrationCert { get; set; }
        public DateTime NextCalibrationDue { get; set; }
        public String ResponsibleSection { get; set; }
        public virtual Contact Contact { get; set; }
        public virtual Equipment Equipment { get; set; }
    }
 public class CalibrationContext:DbContext
    {
        public DbSet<Equipment> Equipments { get; set; }
        public DbSet<Calibration> Calibrations { get; set; }
        public DbSet<Contact> Contacts { get; set; }
        public DbSet<RFQ> RFQs { get; set; }
        public DbSet<RFQDetails> RFQDetails { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }

然后在EquipmentController中我有这个函数

//
        // GET: /Equipment/Details/5
        public ViewResult Details(int id)
        {
            Equipment equipment = db.Equipments.Find(id);
            return View(equipment);
        }

这里是Details View

@model QA.Models.Equipment
@{
    ViewBag.Title = "Details";
}
<h2>Details</h2>
<fieldset>
    <legend>Equipment</legend>
     <div class="display-label">Equipment Name: </div>
      <div class="display-field">@Html.DisplayFor(model => model.EquipName)</div>
    <div class="display-label">EquipDescription: </div>
    <div class="display-field">    @Html.DisplayFor(model => model.EquipDescription)</div>

    <div class="display-label">Serial Number</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.EquipSerialNumber)
    </div>
    <div class="display-label">Image</div>
    <div class="display-field">
              @if(! string.IsNullOrEmpty(Model.EquipImage)){
            <img src="@Url.Content("~/Content/EquipImages/" + System.IO.Path.GetFileName(Model.EquipImage))" alt="" width="70" height="70"/>
            }else{
               <img src="@Url.Content("~/Content/EquipImages/NoImage.jpg")" alt="" width="70" height="70"/>
          }
    </div>
    <div class="display-label">Manufacturor</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.EquipManufacturor)
    </div>
    <div class="display-label">Location</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.EquipLocation)
    </div>
    <div class="display-label">Calibration Frequency</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.EquipCalibrationFreq)
    </div>
    <div class="display-label">
   <b> @Html.LabelFor(model => model.Calibrations)</b>
</div>
<div class="display-field">
    <table>
        <tr>
            <th>Calibration Date</th>
            <th>Calibrated By</th>
            <th>Calibration Cost</th>
            <th>Next Calibration Due on</th>
            <th>Calibration Certificate</th>
        </tr>
        @foreach (var item in Model.Calibrations)
        {
            <tr>
                <td>@Html.DisplayFor(modelItem => item.CalibrationDate)</td>
                <td>@Html.DisplayFor(modelItem => item.Contact.CompanyName)   </td>
                <td>@Html.DisplayFor(modelItem => item.CalibrationCost)</td>
                <td>@Html.DisplayFor(modelItem => item.NextCalibrationDue)</td>
                <td>   @if (item.Contact != null)
                {
                    <a href = @Url.Action("ViewCertificate", new { fileName = item.CalibrationCert }) > <img src = "@Url.Content("~/Content/Icons/pdf.jpg")"  alt = "attachment" /> </a> 
                }</td>

            </tr>
        }
    </table>
</div>

</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.EquipID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

相关内容

  • 没有找到相关文章

最新更新