【问题标题】:Html.RenderPartial() ViewDataDictionary moedl is incorect when running on Azure but works on localhost Net Core 1 appHtml.RenderPartial() ViewDataDictionary 模型在 Azure 上运行时不正确,但适用于 localhost Net Core 1 应用程序
【发布时间】:2018-06-16 02:51:39
【问题描述】:

我有以下情况:

控制器:

[HttpGet]
public async Task<IActionResult> Action()
{
     var actionViewModel = new ActionViewModel()
     {
          ActionOptions = new ActionOptionsViewModel()
     }

     return View(actionViewModel)
}

查看:

@model ActionViewModel

@{ Html.RenderPartial("_ActionOptions", Model.ActionOptions ); }

局部视图:

@model ActionOptionsViewModel

我得到的例外是:

InvalidOperationException:传递到 ViewDataDictionary 的模型项属于“ActionOptionsViewModel”类型,但此 ViewDataDictionary 实例需要“ActionViewModel”类型的模型项。

奇怪的是,仅当我在 Azure 上部署此代码时才会收到此错误,但它在本地工作。任何想法为什么会这样?!我无法在 localhost 上重现错误,并且无法弄清楚在 localhost 和 Azure 上呈现视图时可能会有什么不同......

【问题讨论】:

  • 听起来有些文件没有更新。
  • @juunas 它已更新...部分视图指定了正确的模型..
  • 由于您显示的代码不可能生成该错误,因此最好的猜测是这是由于 POST 方法中的代码导致您返回视图,因为 ModelState 无效(并且您返回 ActionOptionsViewModel到期望ActionViewModel)的主视图
  • @StephenMuecke 我不了解 post 方法。在点击 get 然后在返回的视图中发生错误。如果我故意将部分视图模型更改为“ActionViewModel”,那么我会在本地收到相同的错误。
  • 您显示的代码无法生成该错误。可能的原因请参考this answer

标签: .net asp.net-mvc azure razor asp.net-core


【解决方案1】:

我无法在 localhost 上重现该错误,并且无法弄清楚在 localhost 和 Azure 上呈现视图时可能会有什么不同

我建议您清空 Azure 上的 Web 内容并将应用程序重新发布到 azure 以缩小此问题。对于 Azure Web App,您可以利用 KUDUconnect to your site via ftp 清空 D:\home\site\wwwroot 文件夹下的 Web 内容。另外,您可以Remote debugging web apps。对于 Azure 云服务或 Azure VM,您可以远程访问您的服务器并清空 Web 内容文件夹。

【讨论】:

    猜你喜欢
    • 2020-05-13
    • 2011-09-14
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 2020-01-21
    • 2017-09-04
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多