Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jonathan Claeys 7 posts 87 karma points
    Feb 08, 2022 @ 15:52
    Jonathan Claeys
    0

    How to pass data to a partial view of a Block List Item

    I used to pass data by using the ViewDataDictionary. But in v9 for some reason it isn't working anymore. I'm getting this error: 'ViewDataDictionary' does not contain a constructor that takes 0 arguments

    Does anyone have a solution on how to use the ViewDataDictionary in v9 or how to pass data in another way to my partial?

    Kind regards, Jonathan

  • Marc Love (uSkinned.net) 447 posts 1790 karma points
    Feb 08, 2022 @ 16:03
    Marc Love (uSkinned.net)
    0

    Hi Jonathan,

    In .net core you need to pass ViewData in the constructor

     @await Html.PartialAsync("Path to partial", model, new ViewDataDictionary(ViewData) { { "param1", param1 }, { "param2", param2}})
    
  • Jonathan Claeys 7 posts 87 karma points
    Feb 09, 2022 @ 09:29
    Jonathan Claeys
    0

    I already found that solution but then I'm getting this error:

    ArgumentException: An item with the same key has already been added. Key: param1
    

    Since it is in a loop to run over the block list items.

  • Jonathan Claeys 7 posts 87 karma points
    Feb 09, 2022 @ 09:38
    Jonathan Claeys
    0

    I think I actually found the problem, it's because I'm using another Block List editor in my Block List editor to build up a grid of items. So it's using the Default.cshtml twice to render since it's a nested Block List editor. Do you have any solutions for this?

    Kind regards, Jonathan

  • Marc Love (uSkinned.net) 447 posts 1790 karma points
    Feb 09, 2022 @ 11:02
    Marc Love (uSkinned.net)
    102

    Hi Jonathan,

    If you have several partial views included on your main view the ViewData is cleared on each call. The issue is when you have a partial view call inside a partial view. ViewData isnt cleared so if you are passing ViewData to each partial with the same keys you get this error.

    If you add:

    ViewData.Clear()
    

    before you call the partial you wont get this error message.

    Cheers,

    Marc

  • Jonathan Claeys 7 posts 87 karma points
    Feb 09, 2022 @ 15:41
    Jonathan Claeys
    0

    Perfect! Thanks a lot for your help!

  • Marc Love (uSkinned.net) 447 posts 1790 karma points
    Feb 09, 2022 @ 15:43
    Marc Love (uSkinned.net)
    0

    Hi Jonathan,

    Can you mark last answer as the solution to help other folks that come across this?

    Cheers,

    Marc

  • Amir Khan 1289 posts 2746 karma points
    Oct 08, 2024 @ 21:29
    Amir Khan
    0

    How do you actually retrieve the value of the ViewData in the partial?

  • Amir Khan 1289 posts 2746 karma points
    Oct 08, 2024 @ 21:32
    Amir Khan
    0

    Got it! ViewData["backgroundColor"]

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies