3

I am trying to pass custom data to a Partial View using an instance of ViewDataDictionary

In prior versions the following code worked

@Html.Partial("PartialName", new ViewDataDictionary { { "key", "value" }})

but not in ASP.NET Core.

I have read the documentation but I cannot figure out how to create a working example.

2 Answers 2

2

I used the following code to create a ViewDataDictionary

new ViewDataDictionary(new Microsoft.AspNetCore.Mvc.ModelBinding.EmptyModelMetadataProvider(), new Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary()) { { "key", "value" } }
Sign up to request clarification or add additional context in comments.

Comments

0

In .NET Core that is also possible but you have to use following syntax:

await Html.RenderPartialAsync("PARTIAL_VIEW_NAME")

As you can see bellow that method also has version with ViewDataDictionary parameter: RenderPartialAsync

Also be sure that you Partial View has proper model defined!

4 Comments

Thank you for your reply. Sorry if I was not clear in my question but my problem is in the ViewDataDictionary creation syntax.
Something like that is working fine for me: "new ViewDataDictionary { {"x", "y"} }"
This code gives the following syntax error: 'ViewDataDictionary' does not contain a constructor that takes 0 arguments
I am getting exactly the same error message when rewriting an existing website using VS2015. My code worked OK in VS2010. Have you managed to find a way to successfully display your data?

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.