gpt4 book ai didi

asp.net-mvc-3 - 带有模型的 Razor 布局

转载 作者:行者123 更新时间:2023-12-01 09:37:18 25 4
gpt4 key购买 nike

我有一个布局,它有两个部分 View ,一个用于登录,一个用于注册。

//Login (In layout)
@Html.Partial("_LoginPartial")
and in the partial
@model LogOnVM

///register (in layout)
@Html.Partial("_RegisterPartial")
and in the partial
@model RegisterVM

如图所示,它们都有不同的模型。

我的问题是,当我尝试添加从该布局派生的另一个 View (具有不同模型)时,我收到以下错误

The model item passed into the dictionary is of type 'HomeVM', but this dictionary requires a model item of type 'LogOnVM'.

我怎样才能在布局中渲染局部而不会对其他 View 产生任何问题?

谢谢

最佳答案

嗯,我找到了解决办法。

我没有使用 RenderPartial 调用部分,而是在 Account Controller 中创建了两个 ActionResult 方法:

  1. 一个用于登录(LoginPartial)
  2. 一个用于注册(RegisterPartial)。

然后在布局中我用 RenderAction() 调用它们:

Html.RenderAction("LoginPartial", "Account");
Html.RenderAction("RegisterPartial", "Account");

我不知道这是否是提高性能的最佳方法,但效果很好。

关于asp.net-mvc-3 - 带有模型的 Razor 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5448413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com