来自链接模型的Mvc模型集合引发异常

时间:2017-11-01 16:13:10

标签: c# asp.net-mvc entity-framework

所以我有一个Flora模型,这个Flora模型有一个名为Sensors的集合。但是,当我尝试使用以下代码时,它会抛出异常。

代码:

@model Forester.Models.DAL.Flora

<div id="functionmenu">
    <div class="menubutton">
        <i class="fa fa-home"></i>
    </div>
    <div class="menubutton">
        <i class="fa fa-arrow-left"></i>
    </div>
    <h2>@Model.Name</h2>
    <div class="menubutton fr">
        <i class="fa fa-save"></i>
    </div>
    <div class="clr"></div>
</div>
<div id="editorwrapper">
    @Html.Partial("SensorView", Model.Zone.Sensors())
    <i class="fa fa-plus addrow" data-url='@Url.Action("AddEditRow", "gewas", new { rowView = "SensorView" })'></i>
</div>

例外:

  

对象引用未设置为对象的实例。

以下是我尝试使用的两个模型:

植物:

namespace Forester.Models.DAL
{
using System;
using System.Collections.Generic;

public partial class Zone
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Zone()
    {
        this.Floras = new HashSet<Flora>();
        this.Sensors = new HashSet<Sensor>();
    }

    public int id { get; set; }
    public string Name { get; set; }
    public int ProjectId { get; set; }

    public virtual Project Project { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Flora> Floras { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Sensor> Sensors { get; set; }
}
}

传感器:

namespace Forester.Models.DAL
{
using System;
using System.Collections.Generic;

public partial class Sensor
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Sensor()
    {
        this.SensorValues = new HashSet<SensorValue>();
    }

    public int id { get; set; }
    public int zoneid { get; set; }
    public int sensortypeid { get; set; }
    public int projectid { get; set; }

    public virtual Project Project { get; set; }
    public virtual SensorType SensorType { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<SensorValue> SensorValues { get; set; }
    public virtual Zone Zone { get; set; }
}
}

部分视图加载此视图:

@model Forester.Models.DAL.Sensor

<div class="editorrow">
<div class="sensorname fl divide quarter">
    <h2>@Model.SensorType.name</h2>
    <form id="sensorname">
        <input type="text" value="Sensor name" name="sensornaam" />
    </form>
    <i class="fa fa-pencil editname"></i>
</div>
<div class="sensordata fl divide threequarter">
    <form id="datarow">
        <div class="groups">
            <div class="groupname divide fifth fl">
                <p>Leeftijd</p>
            </div>
            <div class="groupname divide fifth fl">
                <p>Kritiek minimum</p>
            </div>
            <div class="groupname divide fifth fl">
                <p>Kritiek maximum</p>
            </div>
            <div class="groupname divide fifth fl">
                <p>Comfort minimum</p>
            </div>
            <div class="groupname divide fifth fl">
                <p>Comfort maximum</p>
            </div>
            <div class="clr"></div>
        </div>
        @foreach (var thrhold in Model.SensorType.TreshHolds.ToList()) {
            Html.Partial("RijView", thrhold);
        }
        <i class="fa fa-plus addrow" data-url='@Url.Action("AddEditRow", "gewas", new { rowView = "RijView" })'></i>
        <div class="clr"></div>
    </form>
</div>
<i class="fa fa-trash removerow fl"></i>
<div class="clr"></div>
</div>

并根据要求,我的控制器代码:

namespace Forester.Controllers
{
public class GewasController : Controller
{
    private Forester.Models.DAL.foresterEntities1 db = new Models.DAL.foresterEntities1();

    // GET: Gewas
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Bewerken(int id = 1)
    {
        Forester.Models.DAL.Flora gewasData = db.Floras.FirstOrDefault(t => t.id == id);
        return View(gewasData);
    }

    public ActionResult AddEditRow(string rowView)
    {
        return PartialView(rowView);
    }
}
}

有人可以告诉我,我在这里做错了什么吗?

0 个答案:

没有答案