-2

I see lots of these type of questions here, but the answers all seem to point to what I'm doing. Help!!! Here's the pieces of code:

In the MVC5 controller, RegistrationController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Angular4DotNetMvc.Models.Registration;

namespace Angular4DotNetMvc.Controllers
{
    public class RegistrationController : Controller
    {

        private RegistrationVmBuilder _registrationVmBuilder = new RegistrationVmBuilder();

        // GET: Registration
        public ActionResult Index()
        {
            RegistrationVm registration = _registrationVmBuilder.BuildRegistrationVm();
            return View(registration);
        } // end public ActionResult Index()

    } // end public class RegistrationController : Controller

} // end namespace Angular4DotNetMvc.Controllers

The RegistrationVm class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Angular4DotNetMvc.Models.Registration
{

    /// <summary>
    /// Registration View Model Class
    /// </summary>
    public class RegistrationVm
    {
        public string Courses { get; set; }
        public string Instructors { get; set; }
    } // end public class RegistrationVm

} // end namespace Angular4DotNetMvc.Models.Registration

The View, Index.cshtml:

@model Angular4DotNetMvc.Models.Registration.RegistrationVm
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

The RegistrationVmBuilder class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Angular4DotNetMvc.Models.Courses;
using Angular4DotNetMvc.Models.Instructors;

namespace Angular4DotNetMvc.Models.Registration
{

    /// <summary>
    /// Registration View Model Builder Class
    /// </summary>
    public class RegistrationVmBuilder
    {

        /// <summary>
        /// Constructor
        /// </summary>
        /// <returns>A RegistrationVm Instance</returns>
        public RegistrationVm BuildRegistrationVm()
        {
            var registrationVm = new RegistrationVm
            {
                Courses = GetSerializedCourses(),
                Instructors = GetSerializedInstructors()
            };

            return registrationVm;
        } // end public RegistrationVm BuildRegistrationVm()


        /// <summary>
        /// Get Serialized Courses
        /// </summary>
        /// <returns>A JSON string of serialized courses</returns>
        public string GetSerializedCourses()
        {
            var courses = new[]
            {
                new CourseVm {Number = "CREA101", Name = "Care of Magical Creatures", Instructor = "Rubeus Hagrid"},
                new CourseVm {Number = "DARK502", Name = "Defense Against the Dark Arts", Instructor = "Severus Snape"},
                new CourseVm {Number = "TRAN201", Name = "Transfiguration", Instructor = "Minerva McGonagal"}
            };
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var serializedCourses = JsonConvert.SerializeObject(courses, Formatting.None, settings);
            return serializedCourses;
        } // end public string GetSerializedCourses()

        /// <summary>
        /// Get Serialized Instructors
        /// </summary>
        /// <returns>A JSON string of serialized instructors</returns>
        public string GetSerializedInstructors()
        {
            var instructors = new[]
                {
                    new InstructorVm {Name = "Rubeus Hagrid", Email = "hagrid@hogwarts.com", RoomNumber = 1001},
                    new InstructorVm {Name = "Severus Snape", Email = "snape@hogwarts.com", RoomNumber = 105},
                    new InstructorVm {Name = "Minerva McGonagall", Email = "mcgonagall@hogwarts.com", RoomNumber = 102},
                };
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var serializedInstructors = JsonConvert.SerializeObject(instructors, Formatting.None, settings);
            return serializedInstructors;
        } // end public string GetSerializedInstructors()

    } // end public class RegistrationVmBuilder

} // end namespace Angular4DotNetMvc.Models.Registration

When invoking the URL, http:://localhost:3757/Registration I get the following error:

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

Why is a System.String expected when the view declares a RegistrationVm and an instance of that class is passed in the controller?

Thanks.

Peter Gluck
  • 8,168
  • 1
  • 38
  • 37
user3802434
  • 602
  • 1
  • 7
  • 14
  • What line does your error occur at? – mason Apr 27 '15 at 19:30
  • Are you using the `model` somehow in the view? – Christian Apr 27 '15 at 19:36
  • You nee to show you view and any partials that the view may be referencing –  Apr 27 '15 at 22:02
  • I am working through the sample in the Pluralsight course, AngularJS for .NET Developers. I am implementing a MiniSPA following the course video. At this point I have modified the view: – user3802434 Apr 28 '15 at 14:36
  • I am going through the Pluralsight course, AngularJS for .NET Developers, and following their thread of development for the MiniSPA. The exception occurs somewhere between the controller return to display the view and the view instantiation. The view uses a strongly typed model class instead of a string, but somehow it still expects a string as is the case in all the code thus far. In the course sample the model is not referenced in the view only links to other views referencing the model are shown, I put together a simpler project using the same model class that works. – user3802434 Apr 28 '15 at 14:48

1 Answers1

2

I found the problem. It is in the /Shared/_Layout.cshtml file (not shown in my source listing above).

    @model Angular4DotNetMvc.Models.Registration.RegistrationVm
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

The _Layout.cshtml has a "@model string" statement that conflicts with the model, RegistrationVm class, passed from the RegistrationController. To solve the problem, I created another shared file, _StrongLayout.cshtml, without a model and referenced it in the Index.cshtml. This allows any strongly typed model to be passed from a controller.

user3802434
  • 602
  • 1
  • 7
  • 14
  • Worked for me to with a similar problem. MVC5 boilerplate code added a model declaration in the Layout file that wasn't necessary. As soon as I removed it, my page worked just fine. The error message I was getting made no sense to me at all, until I looked at the layout page and any partials used. – ProfNimrod Sep 11 '15 at 16:04