我在这里看到很多这类问题,但答案似乎都指向了我正在做的事情。救命!!!这是代码片段:
在MVC5控制器中,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
RegistrationVm类:
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
View,Index.cshtml:
@model Angular4DotNetMvc.Models.Registration.RegistrationVm
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
RegistrationVmBuilder类:
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
调用网址时,http:://localhost:3757/Registration
我收到以下错误:
传递到字典中的模型项是类型的 &#39; Angular4DotNetMvc.Models.Registration.RegistrationVm&#39;,但是这个 dictionary需要一个类型为&#39; System.String&#39;。
的模型项
当视图声明System.String
并且在控制器中传递该类的实例时,为什么期望RegistrationVm
?
感谢。
答案 0 :(得分:2)
我发现了问题。它位于/Shared/_Layout.cshtml文件中(未在我上面的源代码清单中显示)。
@model Angular4DotNetMvc.Models.Registration.RegistrationVm
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
_Layout.cshtml有一个“@model string”语句,它与从RegistrationController传递的模型RegistrationVm类冲突。为了解决这个问题,我创建了另一个共享文件_StrongLayout.cshtml,没有模型,并在Index.cshtml中引用它。这允许从控制器传递任何强类型模型。