从MVC ASP.NET中的多个表中检索数据 [英] Retrieve Data From multiple tables in MVC ASP.NET
问题描述
我有一个使用我的基础模型制作的ViewModel。我也能够将数据插入表中。
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 .
我的ViewModel由来自2个不同基本模型的属性组成。在视图中,当我尝试使用 ViewModel ,它给出对象未设置为对象的实例错误。
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.
代码如下:
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>
}
当我运行解决方案时,我收到错误:
When I run the solution , I get the error:
无法将对象设置为对象的实例。我将鼠标悬停在foreach循环中的Model上,它显示为Null。
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?
推荐答案
以下是一个示例,U可以尝试这种方法:
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>();
}
}
然后在EquipmentController我有这个功能
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);
}
这里是详情查看
@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>
这篇关于从MVC ASP.NET中的多个表中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!