Error: Model Item passed into dictionary missmatch
Error: Model Item passed into dictionary missmatch
我想制作一个自定义视图模型 class,它采用 2 个模型列表或可枚举模型。当我尝试在视图 class 中显示时,出现以下错误。
传入字典的模型项的类型为“"Prototype01.ViewModels.StockViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IList`1[Prototype01.ViewModels.StockViewModel]"”
我尝试制作单独的视图并使用简单的模型在视图中投影。它工作正常,但是当我尝试获取 viewmodel 时,它并没有像我预期的那样工作。
public class StockController : Controller
{
private ApplicationDbContext _context;
public StockController()
{
_context = new ApplicationDbContext();
}
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
// GET: Stock
public ActionResult StockIndex()
{
var batteries = _context.Batteries.ToList();
var rims = _context.Rims.ToList();
var viewmodel = new StockViewModel
{
Batteries = batteries,
Rims = rims
};
return View(viewmodel);
}
}
public class StockViewModel
{
public IEnumerable<Battery> Batteries { get; set; }
public IEnumerable<Rim> Rims { get; set; }
}
@model IEnumerable<Prototype01.ViewModels.StockViewModel>
@{
ViewBag.Title = "StockIndex";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Stock Management</h2>
@if (!Model.Any())
{
<p>We don't have any Batteries yet.</p>
<label class="btn-group">@Html.ActionLink("Add New", "New", "Battery")
</label>
}
else
{
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>battery ID</th>
<th>battery Name</th>
</tr>
</thead>
<tbody>
@foreach (var bt in Model)
{
<tr>
<td> tying to show batteries here</td>
</tr>
}
</tbody>
</table>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Rim ID</th>
<th>Rim Name</th>
</tr>
</thead>
<tbody>
@foreach (var rm in Model)
{
<tr>
<td> tying to show Rims here</td>
</tr>
}
</tbody>
</table>
}
我正在尝试将数据存储在控制器方法中并发送 viewmodel 以仅显示 2 tables 以及数据库中的 ID 和名称 table 电池和轮辋
另一方面,我遇到了异常
传入字典的模型项是 'Prototype01.ViewModels.StockViewModel' 类型的,但是这个字典需要一个 'System.Collections.Generic.IEnumerable`1[Prototype01.ViewModels.StockViewModel]'.
类型的模型项
您正在迭代模型,而不是单个枚举器; ViewModel 中的电池和轮辋。
所以改行:
@foreach (var bt in Model)
收件人:
@foreach (var bt in Model.Batteries)
你的轮辋也一样:
@foreach (var rm in Model)
收件人:
@foreach (var rm in Model.Rims)
同时更改:
@model IEnumerable<Prototype01.ViewModels.StockViewModel>
至:
@model Prototype01.ViewModels.StockViewModel
我想制作一个自定义视图模型 class,它采用 2 个模型列表或可枚举模型。当我尝试在视图 class 中显示时,出现以下错误。
传入字典的模型项的类型为“"Prototype01.ViewModels.StockViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IList`1[Prototype01.ViewModels.StockViewModel]"”
我尝试制作单独的视图并使用简单的模型在视图中投影。它工作正常,但是当我尝试获取 viewmodel 时,它并没有像我预期的那样工作。
public class StockController : Controller
{
private ApplicationDbContext _context;
public StockController()
{
_context = new ApplicationDbContext();
}
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
// GET: Stock
public ActionResult StockIndex()
{
var batteries = _context.Batteries.ToList();
var rims = _context.Rims.ToList();
var viewmodel = new StockViewModel
{
Batteries = batteries,
Rims = rims
};
return View(viewmodel);
}
}
public class StockViewModel
{
public IEnumerable<Battery> Batteries { get; set; }
public IEnumerable<Rim> Rims { get; set; }
}
@model IEnumerable<Prototype01.ViewModels.StockViewModel>
@{
ViewBag.Title = "StockIndex";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Stock Management</h2>
@if (!Model.Any())
{
<p>We don't have any Batteries yet.</p>
<label class="btn-group">@Html.ActionLink("Add New", "New", "Battery")
</label>
}
else
{
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>battery ID</th>
<th>battery Name</th>
</tr>
</thead>
<tbody>
@foreach (var bt in Model)
{
<tr>
<td> tying to show batteries here</td>
</tr>
}
</tbody>
</table>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Rim ID</th>
<th>Rim Name</th>
</tr>
</thead>
<tbody>
@foreach (var rm in Model)
{
<tr>
<td> tying to show Rims here</td>
</tr>
}
</tbody>
</table>
}
我正在尝试将数据存储在控制器方法中并发送 viewmodel 以仅显示 2 tables 以及数据库中的 ID 和名称 table 电池和轮辋
另一方面,我遇到了异常 传入字典的模型项是 'Prototype01.ViewModels.StockViewModel' 类型的,但是这个字典需要一个 'System.Collections.Generic.IEnumerable`1[Prototype01.ViewModels.StockViewModel]'.
类型的模型项您正在迭代模型,而不是单个枚举器; ViewModel 中的电池和轮辋。
所以改行:
@foreach (var bt in Model)
收件人:
@foreach (var bt in Model.Batteries)
你的轮辋也一样:
@foreach (var rm in Model)
收件人:
@foreach (var rm in Model.Rims)
同时更改:
@model IEnumerable<Prototype01.ViewModels.StockViewModel>
至:
@model Prototype01.ViewModels.StockViewModel