我正在整理一个相当简单的Code-First MVC5库存跟踪应用程序。我已将我的应用程序发送到Seed()和所有维护表(位置,供应商,状态等),我可以查看/创建/编辑/删除。我现在正在处理我的主[INV_Assets]模型的View
,但是当我尝试通过模型中的项foreach
来显示表中的所有[INV_Assets]时,我得到错误: foreach statement cannot operate on variables of type 'Tracker.Models.INV_Assets' because 'Tracker.Models.INV_Assets' does not contain a public definition for 'GetEnumerator'
。
以下是我[INV_Assets]
的模型:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using GridMvc.DataAnnotations;
using System.Web.Mvc;
using Tracker.Models;
namespace Tracker.Models
{
[GridTable(PagingEnabled = true, PageSize = 30)]
public class INV_Assets
{
// Setting GridColumn Annotations allows you to use AutoGenerateColumns on view to auto create the Grid based on the model.
public int Id { get; set; }
public int Model_Id { get; set; }
[ForeignKey("Model_Id")]
public virtual INV_Models Model { get; set; }
[Required]
public int Manufacturer_Id { get; set; }
[ForeignKey("Manufacturer_Id")]
public virtual INV_Manufacturers Manufacturer { get; set; }
[Required]
public int Type_Id { get; set; }
[ForeignKey("Type_Id")]
public virtual INV_Types Type { get; set; }
[Required]
public int Location_Id { get; set; }
[ForeignKey("Location_Id")]
public virtual INV_Locations Location { get; set; }
public int Vendor_Id { get; set; }
[ForeignKey("Vendor_Id")]
public virtual INV_Vendors Vendor { get; set; }
[Required]
public int Status_Id { get; set; }
[ForeignKey("Status_Id")]
public virtual INV_Statuses Status { get; set; }
public string ip_address { get; set; }
public string mac_address { get; set; }
public string note { get; set; }
public string owner { get; set; }
public decimal cost { get; set; }
public string po_number { get; set; }
public string description { get; set; }
public int invoice_number{ get; set; }
[Required]
public string serial_number { get; set; }
[Required]
public string asset_tag_number { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? acquired_date { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? disposed_date { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime created_date { get; set; }
[Required]
public string created_by { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime modified_date { get; set; }
public string modified_by { get; set; }
// Flag to specify if item is available? (Not signed out, not auctioned, recycled, etc.)
//public bool available { get; set; }
}
}
[INV_Assets]查看:
@using GridMvc.Html
@model Tracker.Models.INV_Assets
@{
ViewBag.Title = "Home Page";
}
<table style="width:100%;">
@foreach (var item in Model) {
<tr>
<td>@Html.DisplayFor(modelItem => item.Status)</td>
<td>@Html.DisplayFor(modelItem => item.Location)</td>
<td>@Html.DisplayFor(modelItem => item.owner)</td>
<td>@Html.DisplayFor(modelItem => item.Type)</td>
<td>@Html.DisplayFor(modelItem => item.Manufacturer)</td>
<td>@Html.DisplayFor(modelItem => item.Model)</td>
<td>@Html.DisplayFor(modelItem => item.Vendor)</td>
<td>@Html.DisplayFor(modelItem => item.description)</td>
<td>@Html.DisplayFor(modelItem => item.asset_tag_number)</td>
<td>@Html.DisplayFor(modelItem => item.serial_number)</td>
<td>@Html.DisplayFor(modelItem => item.ip_address)</td>
<td>@Html.DisplayFor(modelItem => item.mac_address)</td>
<td>@Html.DisplayFor(modelItem => item.po_number)</td>
<td>@Html.DisplayFor(modelItem => item.invoice_number)</td>
<td>@Html.DisplayFor(modelItem => item.cost)</td>
<td>@Html.DisplayFor(modelItem => item.note)</td>
<td>@Html.DisplayFor(modelItem => item.acquired_date)</td>
<td>@Html.DisplayFor(modelItem => item.disposed_date)</td>
<td>@Html.DisplayFor(modelItem => item.created_date)</td>
<td>@Html.DisplayFor(modelItem => item.created_by)</td>
<td>@Html.DisplayFor(modelItem => item.modified_date)</td>
<td>@Html.DisplayFor(modelItem => item.modified_by)</td>
</tr>
}
</table>
我不太确定如何解决这个错误,并想知道有经验的人是否可以称重?
我在[INV_Locations]
模型/视图中做同样的事情时,我没有遇到任何问题(不确定原因):
[INV_Locations] :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Tracker.Models
{
public class INV_Locations
{
public int Id { get; set; }
public string location_dept { get; set; }
public string location_room { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime created_date { get; set; }
[Required]
public string created_by { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime modified_date { get; set; }
public string modified_by { get; set; }
}
}
[INV_Locations]查看:
@model IEnumerable<Tracker.Models.INV_Locations>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h3>Maintenance - Locations</h3>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th></th>
<th>
@*@Html.DisplayNameFor(model => model.location_dept)*@
Dept:
</th>
<th>
Room:
</th>
<th>
Created:
</th>
<th>
By:
</th>
<th>
Modified:
</th>
<th>
By:
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
<td>
@Html.DisplayFor(modelItem => item.location_dept)
</td>
<td>
@Html.DisplayFor(modelItem => item.location_room)
</td>
<td>
@Html.DisplayFor(modelItem => item.created_date)
</td>
<td>
@Html.DisplayFor(modelItem => item.created_by)
</td>
<td>
@Html.DisplayFor(modelItem => item.modified_date)
</td>
<td>
@Html.DisplayFor(modelItem => item.modified_by)
</td>
</tr>
}
</table>
我尝试在我的[INV_Assets] @model Tracker.Models.INV_Assets
上将@model IEnumerable<Tracker.Models.INV_Assets>
更改为View
(就像我的[INV_Locations] View
拥有它一样。这使我的解决方案可以构建,但是当我尝试将我的应用程序运行到View
时,我会收到:
System.InvalidOperationException
The model item passed into the dictionary is of type 'Tracker.Models.INV_Assets', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1[Tracker.Models.INV_Assets]'.
编辑 - 关于Shyju的建议:
我更改了HomeController
Index()
:
TrackerContext _db = new TrackerContext();
public ActionResult Index(INV_Assets defModel)
{
return View(defModel);
}
为:
TrackerContext _db = new TrackerContext();
public ActionResult Index(INV_Assets defModel)
{
var assetList = new List<Tracker.Models.INV_Assets>();
assetList.Add(defModel);
return View(assetList);
}
现在我的View加载了,我得到的表格HTML就是:
<table style="width:100%;">
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>0</td>
<td>0.00</td>
<td></td>
<td></td>
<td></td>
<td>01/01/0001</td>
<td></td>
<td>01/01/0001</td>
<td></td>
</tr>
</table>
我已经确认我的DBContext中有2条完整的[INV_Assets]记录,但是它们没有显示在View中?如何正确地将[INV_Assets]模型的所有实例加载到新的assetList
变量中?
EDIT2 :
将我的Index()
修改为:
TrackerContext _db = new TrackerContext();
public ActionResult Index(INV_Assets defModel)
{
var assetList = _db.INV_Assets.ToList(); // EXCEPTION
return View(assetList);
}
但是现在,在构建解决方案时,当我尝试运行它时,应用程序失败了。详情如下:
错误:An exception of type 'System.Data.DataException' occurred in EntityFramework.dll but was not handled in user code. Additional information: An exception occurred while initializing the database. See the InnerException for details.
消息:An exception occurred while initializing the database. See the InnerException for details.
InnerException :{"The underlying provider failed on Open."}
来源:EntityFramework
答案 0 :(得分:1)
您的视图绑定到Tracker.Models.INV_Assets
的单个实例。但在你的视图中,你正试图循环它。您需要确保传递给视图的内容是一个集合,以便我们可以遍历它。
在INV Assets视图中,更改
@model Tracker.Models.INV_Assets
到
@model List<Tracker.Models.INV_Assets>
此外,您需要确保从操作方法中传递INV_Assets
个对象的集合。
public ActionResult INVAssets()
{
var assetList=new List<Tracker.Models.INV_Assets>();
// Ex : manually adding one record. replace with your actual code to load data
// to do : Add item to the list now
assetList.Add(new INV_Assets { Name ="Test"});
return View(assetList);
}
修改 根据问题中的修改
您的Index
操作不应将INV_Assets
的对象作为参数。
TrackerContext _db = new TrackerContext();
public ActionResult Index()
{
var assetList =_db.INV_Assets.ToList();
return View(assetList);
}
假设INV_Assets
是您TrackerContext
课程的属性。如果您的属性名称不同,请更改上面的代码以反映该属性。
如果要加载特定位置的资源,请更新Index操作方法以接受位置ID作为参数。
public ActionResult Index(int id)
{
var assetList =_db.INV_Assets.Where(s=>s.Location_Id==id).ToList();
return View(assetList);
}
因此,您访问此页面的网址就像
http://yoursitename/YourControllerName/Index/5
其中5
是一个有效的位置ID,其中包含一些与之关联的资产。