2

I am trying to run my first ASP.NET MVC application. I created a controller and view. Data is taken from the Database.

Here is the view


@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>

Here is my controller code.

namespace WebApplication4.Controllers
{
    public class UserController : Controller
    {
        private IUserRepository repository;
        public UserController(IUserRepository repo)
        { repository = repo; }
        public ViewResult List() => View(repository.Users);

    }
}

However, when the project can run but when I try to navigate User page I get the following error:

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'.

Natali
  • 35
  • 1
  • 5
  • Yes, Sorry, i did it – Natali Aug 30 '19 at 04:50
  • 1
    Nobody ever reads exception messages :( _"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'."_ means that the model item that you pass into `ViewDataDictionary` is of type `InternalDbSet`, but this `ViewDataDictionary` is expecting model of type `User`. See? `typeof(InternalDbSet)` != `typeof(User)` – vasily.sib Aug 30 '19 at 05:06
  • @vasily.sib indeed it's a rather self explanatory error message, but I suspect the confusion is caused by having two different classes both called user and not a strong appreciation for the difference :) – Caius Jard Aug 30 '19 at 05:14
  • @CaiusJard there is only one `User` class here. The problem is that they pass `repository.Users` (which is of type `InternalDbSet`) to `View(..)` method in controller and specifing `WebApplication4.Models.User` type as `@model` for view. And this is exactly what exception message is telling. But nobody ever reads exception messages :( – vasily.sib Aug 30 '19 at 05:18
  • Possible duplicate of [The model item passed into the dictionary is of type .. but this dictionary requires a model item of type](https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) – Caius Jard Aug 30 '19 at 05:58

1 Answers1

1

At the top of your page you've told the runtime to expect a model of a single instance of your user:

@model WebApplication4.Models.User

And then in your controller you've dumped a collection of User objects into it:

public ViewResult List() =>  View(repository.Users);

Just put a single user in:

public ViewResult List() =>  View(repository.Users.First());

Or tell the view the @model is multiple users:

@model IEnumerable<WebApplication4.Models.User>

-

Side note, please read What is ViewModel in MVC?

Caius Jard
  • 72,509
  • 5
  • 49
  • 80