【问题标题】:asp.net Core 1 RenderPartial ViewDataDictionaryasp.net Core 1 RenderPartial ViewDataDictionary
【发布时间】:2017-03-20 15:43:52
【问题描述】:

我正在用 VS 2017 重写一个最初用 VS 2010 编写的网站。我最初用于在页脚中显示访问者数量的命令是:- @Html.RenderPartial( "/Counter/Counter.ascx", new ViewDataDictionary {{ "digits", 6 }, { "id", "Count" }})。这将 Count.txt 中的数字显示为 6 位数字,使用 digits.gif 作为模式。当我尝试在 VS 2017 中使用相同的代码时,我收到以下错误消息:'ViewDataDictionary' does not contain a constructor that takes 0 arguments。我花了很多时间在这个论坛和其他网站上查看类似的问题,但一直找不到答案。您能给我的任何帮助将不胜感激。

【问题讨论】:

    标签: asp.net-core-1.0 renderpartial


    【解决方案1】:

    另一种使用方法是将当前视图的ViewData 传递给构造函数。这样,新的ViewDataDictionary 就可以使用您使用集合初始化器放入的项目进行扩展。

    @Html.Partial("MyPartial", new ViewDataDictionary(ViewData) { { "digits", 6 } })
    

    【讨论】:

      【解决方案2】:

      当我去实现 ViewDataDictionaryo 时:

      namespace Microsoft.AspNetCore.Mvc.ViewFeatures
      {
          public ViewDataDictionary(IModelMetadataProvider metadataProvider,
                                                ModelStateDictionary modelState);
      }
      

      可能的解决方案

          new ViewDataDictionary(
       new Microsoft.AspNetCore.Mvc.ModelBinding.EmptyModelMetadataProvider(),
       new Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary())
       {{ "digits", 6 }, { "id", "Count" }}
      

      【讨论】:

      • 感谢您的回复。首先,我假设我需要 在您的代码前面。如果我不这样做,我会在第二个“新”语句之前收到“Type Expected”错误。如果我使用这个语句 我收到一条错误消息,上面写着“'ModelStateDictionary' 不包含 'Add' 的定义......”
      猜你喜欢
      • 2023-03-17
      • 2018-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-10
      • 1970-01-01
      相关资源
      最近更新 更多