2013-02-28 42 views
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.

因此,我應該寫在這裏,以便從兩個表顯示數據?

+1

後一些代碼請。 – 2013-02-28 09:53:37

+0

顯示您的代碼... – 2013-02-28 09:53:39

回答

0

這裏有一個例子,U可以試試這個方法:

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); 
     } 

,這裏是詳細信息視圖

@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>