qq_15235719 2020-05-12 22:26 采纳率: 0%
浏览 710
已结题

C#异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

通过MVC 模板创建视图提示此类错误,

Model是通过引用EF6框架

控制器代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Asset.Models;
        public ActionResult AssetList()
        {
            IList<UsbBorrow> UsbBorrows = null;
            using (AssetEntities db = new AssetEntities())
            {
                db.UsbBorrow.Select(S => S).ToList();
            }
            return View(UsbBorrows);
        }

视图代码

@model IEnumerable<Asset.Models.UsbBorrow>

@{
    ViewBag.Title = "AssetList";
}

<h2>AssetList</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Type)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Specification)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ProductName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Num)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ReceiveDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ReturnDate)
        </th>
        <th></th>
    </tr>

@foreach (Asset.Models.UsbBorrow UsbBorrow in Model ) {

    <tr>
        <td>
            @Html.DisplayFor(modelItem => UsbBorrow.Type)
        </td>
        <td>
            @Html.DisplayFor(modelItem => UsbBorrow.Specification)
        </td>
        <td>
            @Html.DisplayFor(modelItem => UsbBorrow.ProductName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => UsbBorrow.Num)
        </td>
        <td>
            @Html.DisplayFor(modelItem => UsbBorrow.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => UsbBorrow.ReceiveDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => UsbBorrow.ReturnDate)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id= UsbBorrow.ID }) |
            @Html.ActionLink("Details", "Details", new { id= UsbBorrow.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id= UsbBorrow.ID })
        </td>
    </tr>
}

</table>

Foreach的时候报错

  • 写回答

3条回答 默认 最新

  • threenewbee 2020-05-12 23:45
    关注

    db.UsbBorrow.Select(S => S).ToList();
    ->
    UsbBorrows = db.UsbBorrow.Select(S => S).ToList();

    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效