0

I am re-writing a website in VS 2017 that was originally written in VS 2010. The command I used originally to display my visitor count in the footer was:- @Html.RenderPartial( "/Counter/Counter.ascx", new ViewDataDictionary {{ "digits", 6 }, { "id", "Count" }}). This displayed the number in Count.txt as 6 digits using digits.gif as the pattern. When I try and use the same code in VS 2017, I get the following error message: 'ViewDataDictionary' does not contain a constructor that takes 0 arguments. I have spent a lot of time looking at similar questions in this forum and on other sites and have been unable to find an answer. Any help you can give me will be most appreciated.

2 Answers 2

3

Another way to use this is to pass the ViewData of the current view into the constructor. That way the new ViewDataDictionary gets extended with the items you put in using the collection initializer.

@Html.Partial("MyPartial", new ViewDataDictionary(ViewData) { { "digits", 6 } })
Sign up to request clarification or add additional context in comments.

Comments

1

When i went to implementation of the ViewDataDictionaryo:

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

Possible solution

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

1 Comment

Thank you for your response. Firstly, I assume I need <@Html.RenderPartial( "/Counter/Counter.ascx" > in front of your code. If I don't, I get a "Type Expected" error before the second 'new' statement. If I use this statement <@Html.RenderPartial("wwwroot/Counter/Counter.ascx", new ViewDataDictionary(new Microsoft.AspNetCore.Mvc.ModelBinding.EmptyModelMetadataProvider(), new Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary() {{ "digits", 6 }, { "id", "Count" }}));> I get an error message that says "'ModelStateDictionary' does not contain a definition for 'Add' ... "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.