如何修复 InvalidOperationException:传入 ViewDataDictionary 的模型项属于类型
How to fix InvalidOperationException: The model item passed into the ViewDataDictionary is of type
我正在尝试 运行 我的第一个 ASP.NET MVC 应用程序。我创建了一个控制器和视图。数据取自数据库。
这是风景
@model WebApplication4.Models.User
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>会員情報画面</title>
</head>
<body>
@using (Html.BeginForm())
{
<style type="text/css">
table {
border-collapse: collapse;
}
th {
background: #ccc;
text-align: left;
}
td, th {
border: 1px solid #800;
padding: 4px;
}
</style>
<table>
<tr>
<th style="background: #ffffff; border: 1px solid #200;">
<p> 検索用 </p>
</th>
<th style="background: #ffffff ;border: 1px solid #200;">
<p> 追加・更新・削除 </p>
</th>
</tr>
<tr>
<th>
<p>
会員コード: @Html.TextBoxFor(x => x.numer)
会員名: @Html.TextBoxFor(x => x.name)
</p>
<p>
@Html.CheckBoxFor(m => m.sexcb, false) @Html.Label("男性")
@Html.CheckBoxFor(m => m.sexcb, false) @Html.Label("女性")
@Html.CheckBoxFor(m => m.sexcb, false) @Html.Label("その他")
</p>
<p>生年月日: <input type="date" name="calendar" min="1900-01-01" max="2019-01-01"> ~ <input type="date" name="calendar" min="1900-01-01" max="2019-01-01"></p>
<p>ポイント: @Html.TextBoxFor(x => x.point) ~ @Html.TextBoxFor(x => x.point)</p>
</th>
<th>
<p>
会員コード: @Html.TextBoxFor(x => x.numer)
会員名: @Html.TextBoxFor(x => x.name)
</p>
<p>
@Html.RadioButtonFor(model => model.sex, "T") @Html.Label("男性")
@Html.RadioButtonFor(model => model.sex, "C") @Html.Label("女性")
@Html.RadioButtonFor(model => model.sex, "C") @Html.Label("その他")
</p>
<p>生年月日: <input type="date" name="calendar" min="1900-01-01" max="2019-01-01"></p>
<p>ポイント: @Html.TextBoxFor(x => x.point)</p>
</th>
</tr>
</table>
<h1></h1>
<table cellspacing="0" border="1">
<tr>
<th></th>
<th colspan="1">会員コード</th>
<th colspan="1">会員名</th>
<th colspan="1">会員名カナ</th>
<th colspan="1">会員性別</th>
<th colspan="1">会員生年月日</th>
<th colspan="1">会員ポイント</th>
<th colspan="1">バージョン</th>
</tr>
</table>
@RenderBody()
<p>
<input style="padding: 20px;" type="submit" value="検索" />
<input style="padding: 20px;" type="submit" value="クリア" />
<input style="padding: 20px;" type="submit" value="追加" />
<input style="padding: 20px;" type="submit" value="更新" />
<input style="padding: 20px;" type="submit" value="削除" />
<input style="padding: 20px;" type="submit" value="ビデオ情報画面" />
</p>
}
</body>
</html>
这是我的控制器代码。
namespace WebApplication4.Controllers
{
public class UserController : Controller
{
private IUserRepository repository;
public UserController(IUserRepository repo)
{ repository = repo; }
public ViewResult List() => View(repository.Users);
}
}
但是,当项目可以 运行 但当我尝试导航用户页面时,我收到以下错误:
InvalidOperationException: The model item passed into the
ViewDataDictionary is of type
'Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1[WebApplication4.Models.User]',
but this ViewDataDictionary instance requires a model item of type
'WebApplication4.Models.ViewModels.UserListViewModel'.
在您页面的顶部,您已经告诉运行时期望您的用户的单个实例的模型:
@model WebApplication4.Models.User
然后在您的控制器中将一组用户对象转储到其中:
public ViewResult List() => View(repository.Users);
只需将单个用户放入:
public ViewResult List() => View(repository.Users.First());
或者告诉视图 @model 是多个用户:
@model IEnumerable<WebApplication4.Models.User>
-
旁注,请阅读What is ViewModel in MVC?
我正在尝试 运行 我的第一个 ASP.NET MVC 应用程序。我创建了一个控制器和视图。数据取自数据库。
这是风景
@model WebApplication4.Models.User
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>会員情報画面</title>
</head>
<body>
@using (Html.BeginForm())
{
<style type="text/css">
table {
border-collapse: collapse;
}
th {
background: #ccc;
text-align: left;
}
td, th {
border: 1px solid #800;
padding: 4px;
}
</style>
<table>
<tr>
<th style="background: #ffffff; border: 1px solid #200;">
<p> 検索用 </p>
</th>
<th style="background: #ffffff ;border: 1px solid #200;">
<p> 追加・更新・削除 </p>
</th>
</tr>
<tr>
<th>
<p>
会員コード: @Html.TextBoxFor(x => x.numer)
会員名: @Html.TextBoxFor(x => x.name)
</p>
<p>
@Html.CheckBoxFor(m => m.sexcb, false) @Html.Label("男性")
@Html.CheckBoxFor(m => m.sexcb, false) @Html.Label("女性")
@Html.CheckBoxFor(m => m.sexcb, false) @Html.Label("その他")
</p>
<p>生年月日: <input type="date" name="calendar" min="1900-01-01" max="2019-01-01"> ~ <input type="date" name="calendar" min="1900-01-01" max="2019-01-01"></p>
<p>ポイント: @Html.TextBoxFor(x => x.point) ~ @Html.TextBoxFor(x => x.point)</p>
</th>
<th>
<p>
会員コード: @Html.TextBoxFor(x => x.numer)
会員名: @Html.TextBoxFor(x => x.name)
</p>
<p>
@Html.RadioButtonFor(model => model.sex, "T") @Html.Label("男性")
@Html.RadioButtonFor(model => model.sex, "C") @Html.Label("女性")
@Html.RadioButtonFor(model => model.sex, "C") @Html.Label("その他")
</p>
<p>生年月日: <input type="date" name="calendar" min="1900-01-01" max="2019-01-01"></p>
<p>ポイント: @Html.TextBoxFor(x => x.point)</p>
</th>
</tr>
</table>
<h1></h1>
<table cellspacing="0" border="1">
<tr>
<th></th>
<th colspan="1">会員コード</th>
<th colspan="1">会員名</th>
<th colspan="1">会員名カナ</th>
<th colspan="1">会員性別</th>
<th colspan="1">会員生年月日</th>
<th colspan="1">会員ポイント</th>
<th colspan="1">バージョン</th>
</tr>
</table>
@RenderBody()
<p>
<input style="padding: 20px;" type="submit" value="検索" />
<input style="padding: 20px;" type="submit" value="クリア" />
<input style="padding: 20px;" type="submit" value="追加" />
<input style="padding: 20px;" type="submit" value="更新" />
<input style="padding: 20px;" type="submit" value="削除" />
<input style="padding: 20px;" type="submit" value="ビデオ情報画面" />
</p>
}
</body>
</html>
这是我的控制器代码。
namespace WebApplication4.Controllers
{
public class UserController : Controller
{
private IUserRepository repository;
public UserController(IUserRepository repo)
{ repository = repo; }
public ViewResult List() => View(repository.Users);
}
}
但是,当项目可以 运行 但当我尝试导航用户页面时,我收到以下错误:
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1[WebApplication4.Models.User]', but this ViewDataDictionary instance requires a model item of type 'WebApplication4.Models.ViewModels.UserListViewModel'.
在您页面的顶部,您已经告诉运行时期望您的用户的单个实例的模型:
@model WebApplication4.Models.User
然后在您的控制器中将一组用户对象转储到其中:
public ViewResult List() => View(repository.Users);
只需将单个用户放入:
public ViewResult List() => View(repository.Users.First());
或者告诉视图 @model 是多个用户:
@model IEnumerable<WebApplication4.Models.User>
-
旁注,请阅读What is ViewModel in MVC?