Startup.ConfigureServices
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
Startup.Configure
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
视图/ 首页 /Index.cshtml
@model FooViewModel
@{
ViewData["Title"] = "Home page title";
}
<div class="row mt-2">
@Html.DisplayForModel()
</div>
视图/ 选项 /Index.cshtml
@model BarViewModel
@{
ViewData["Title"] = "Other page title";
}
<div class="row mt-2">
@Html.DisplayForModel()
</div>
OptionsController
public class OptionsController : BaseController
{
public OptionsController(){}
public IActionResult Index() => View(new BarViewModel());
}
在OptionsController.Index()
处达到断点后,我收到一条错误消息,指出:
InvalidOperationException:传递到ViewDataDictionary的模型项的类型为'BarViewModel',但是此ViewDataDictionary实例需要类型为'FooViewModel'的模型项。
...因为在我的FooViewModel
中使用了Views/Home/Index.cshtml
,所以我认为ViewEngine或其他正在查看Views / Home /Index.cshtml文件的视图而不是Views / 选项 /Index.cshtml。
有什么建议吗?我想念什么?