0

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

我现在想检索数据并显示在我的表单上。我应该在我的Index View中编写什么代码。

我的 ViewModel 由来自 2 个不同基本模型的属性组成。因此,在视图中,当我尝试使用我的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>
}

当我运行解决方案时,我收到错误:

对象不能设置为对象的实例。我将鼠标悬停在 foreach 循环中的“模型”上,它显示为 Null。

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

4

1 回答 1

0

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

 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>
于 2013-02-28T10:47:51.243 回答