我收到以下错误消息。有人可以告诉我为什么我收到以下错误消息。调试控制器时,我的模型正在使用值进行初始化。
The model item passed into the dictionary is of type 'AngularJSMvcExample.Models.RegistrationVm', but this dictionary requires a model item of type 'System.String'.
我的型号代码
namespace AngularJSMvcExample.Models
{
public class RegistrationVm
{
public string Courses { get; set; }
public string Instructors { get; set; }
}
}
我的控制器代码
namespace AngularJSMvcExample.Controllers
{
public class RegistrationController : Controller
{
private RegistrationVmBuilder _registrationVmBuilder = new RegistrationVmBuilder();
// GET: Registration
public ActionResult Index()
{
return View(_registrationVmBuilder.BuildRegistrationVm());
}
}
}
我的ViewCode
@model AngularJSMvcExample.Models.RegistrationVm
@{
ViewBag.Title = "Registration";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="container" ng-controller="RegistrationController">
<div class="row">
<div class="navbar navbar-default">
<div class="navbar-header">
<ul class="nav navbar-nav">
<li>
<span class="navbar-brand">Registration</span>
</li>
</ul>
</div>
<div class="navbar-collapse collapse">
<ul class="nav nav-bar">
<li ><a href="/Courses">Browse Catalog</a></li>
<li><a href="/Instructors">Browse Instructors</a></li>
</ul>
</div>
</div>
</div>
</div>
我的RegistrationVmBuilder代码
namespace AngularJSMvcExample.Models
{
public class RegistrationVmBuilder
{
public RegistrationVm BuildRegistrationVm()
{
var registrationVm = new RegistrationVm
{
Courses = GetSerialisedCourse(),
Instructors = GetSerialisedInstructors()
};
return registrationVm;
}
public string GetSerialisedCourse()
{
var courses = new[]
{
new CourseVm {Number= "100", Name= "Physis", Instructor = "Jan"},
new CourseVm {Number= "101", Name= "Chemistry", Instructor = "Sal"},
new CourseVm {Number= "102", Name= "Biology", Instructor = "San"},
new CourseVm {Number= "103", Name= "History", Instructor = "Jack"},
new CourseVm {Number= "104", Name= "Maths", Instructor = "Rahul"}
};
var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
var serializeCourses = JsonConvert.SerializeObject(courses, settings);
return serializeCourses;
}
public string GetSerialisedInstructors()
{
var instructors = new[]
{
new InstructorsVm {Name= "Jan", Email= "jan.test@test.com", Roomno = "10"},
new InstructorsVm {Name= "Pal", Email= "pal.test@test.com", Roomno = "9"},
new InstructorsVm {Name= "San", Email= "san@test.com", Roomno = "11"},
new InstructorsVm {Name= "Jack", Email= "jack@test@test.com", Roomno = "12"},
new InstructorsVm {Name= "Rahul", Email= "rahul@test@test.com", Roomno = "15"}
};
var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
var serializeInstructors = JsonConvert.SerializeObject(instructors, settings);
return serializeInstructors;
}
}
}
答案 0 :(得分:0)
您确定这是索引视图吗?
@model AngularJSMvcExample.Models.RegistrationVm
@{
ViewBag.Title = "Registration";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="container" ng-controller="RegistrationController">
<div class="row">
<div class="navbar navbar-default">
<div class="navbar-header">
<ul class="nav navbar-nav">
<li>
<span class="navbar-brand">Registration</span>
</li>
</ul>
</div>
<div class="navbar-collapse collapse">
<ul class="nav nav-bar">
<li ><a href="/Courses">Browse Catalog</a></li>
<li><a href="/Instructors">Browse Instructors</a></li>
</ul>
</div>
</div>
</div>