问题
I have a ViewModel which I have made using my base Models. I am also able to insert data into the tables.
I want to now retrieve the data and show on my form. What code should I write in my Index View .
My ViewModel is composed of properties from 2 different base models .Hence, in the view , when i try to get information using properties from my ViewModel , it gives the "Object not set to an instance of an object" Error.
Codes are as Follows :
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; }
}
Base Model Classes:
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; }
}
Index View :
@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>
}
When I run the solution , I get the error:
Object cannot be set to an instance of an object. I hover over the "Model" in the foreach loop and it shows as Null.
Hence, what should I write here so that to display data from both tables?
回答1:
Here is a sample, U can try this approach:
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>();
}
}
and then in EquipmentController I have this function
//
// GET: /Equipment/Details/5
public ViewResult Details(int id)
{
Equipment equipment = db.Equipments.Find(id);
return View(equipment);
}
and here is 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>
来源:https://stackoverflow.com/questions/15132373/retrieve-data-from-multiple-tables-in-mvc-asp-net