2015-06-21 44 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

只需從共享佈局中刪除模型聲明。

相關問題