2015-06-21 83 views
0

我有Registration控制器:传递到字典的模型是类型“TestApp.Models.Registration.RegistrationVm”,但这部词典需要System.String类型的模型项目

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

} 

RegistrationBuilder类:

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

RegistrationVm类:

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> 

而且在索引视图:

@model TestApp.Models.Registration.RegistrationVm

问题是,当我跑我得到这个错误的程序:

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

+0

你的哪行代码会得到这个错误? – alisabzevari

+0

@alisabzevari在注册网址中运行应用程序后。 – user2019037

+0

你是什么意思的注册网址? – alisabzevari

回答

2

shared _Layout.cshtml有声明的模型(@model string),它由任何使用它的视图继承。所以基本上你的索引视图的模型(@model TestApp.Models.Registration.RegistrationVm)被忽略。

我不知道你为什么在你的_Layout视图中有@model string(模型似乎没有用在那里),但是如果你想使用继承,请参阅Pass data to layout that are common to all pages

只需从共享布局中删除模型声明。

相关问题