0
votes

I have the Registration controller:

public class RegistrationController : Controller
{
    private RegistrationVmBuilder _registrationVmBuilder = new RegistrationVmBuilder();
    public ActionResult Index()
    {
        return View(_registrationVmBuilder.BuildRegistrationVm());
    }

}

and the RegistrationBuilder class:

public class RegistrationVmBuilder
{
    public RegistrationVm BuildRegistrationVm()
    {
        RegistrationVm registrationVm = new RegistrationVm
        {
            Courses = GetSerializedCourseVms(),
            Instructors = GetSerializedInstructors()
        };
        return registrationVm;
    }
}

RegistrationVm class:

public class RegistrationVm
{
    public string Courses { get; set; }
    public string Instructors { get; set; }
}

_Layout.cshtml:

@model string
<html ng-app="registrationModule">
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
    <script src="~/Scripts/jquery-2.1.4.min.js"></script>
    <script src="~/Scripts/bootstrap.js"></script>
    <script src="~/Scripts/registration-module.js"></script>
    @RenderSection("JavascriptInHead")
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
    <link href="~/Content/bootstrap-state.min.css" rel="stylesheet" />
    <title>College Registration</title>
</head>
<body>
    @RenderBody()
</body>
</html>

And in index view:

@model TestApp.Models.Registration.RegistrationVm

The problem is when i run the app I got this error:

The model item passed into the dictionary is of type 'TestApp.Models.Registration.RegistrationVm', but this dictionary requires a model item of type 'System.String'.

1
What line of your code gets this error?alisabzevari
@alisabzevari after I run the application in registration url.user2019037
What do you mean by registration url?alisabzevari
For example http://localhost:30460/registrationuser2019037
You have to trace your application from the start of you controller action to find where the error occurs.alisabzevari

1 Answers

2
votes

The shared _Layout.cshtml has a model declared (@model string), which is inherited by any view that uses it. So essentially your Index view's model (@model TestApp.Models.Registration.RegistrationVm) is ignored.

I don't know why you have @model string in your _Layout view (the model doesn't seem to be used in there), but if you want to use inheritance see Pass data to layout that are common to all pages.

Simply remove the model declaration from the shared layout.