Copied to clipboard

Flag this post as spam?

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


  • Colm Garvey 40 posts 65 karma points
    Nov 15, 2016 @ 01:04
    Colm Garvey
    0

    Rendering a dynamic Partial View?

    Hi all,

    I'm using the following to render a dynamic list of services:

    @{
            if (Model.Content.HasValue("checkBoxServices"))
            {
                foreach (var item in Model.Content.GetPropertyValue<string>("checkBoxServices").Split(','))
                {
                    Html.RenderPartial("practiceServices"/@item);
                }
            }
        }
    

    The idea is that each partial will contain information on one service. It works perfectly if the partial is in the root partial directory, but there are going to be a lot of items and I want to keep them in their own "practiceServices" folder. However... this is throwing an error and I'm not sure why. Can anyone suggest a solution?

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Nov 15, 2016 @ 01:11
    Marc Goodson
    100

    Hi Colm

    Try:

    Html.RenderPartial("~/Views/Partials/practiceServices/" + item + ".cshtml")
    

    ie by giving direct path to the razor file it should work! - when the direct path isn't specified, MVC will look in the folders it 'knows' the file might reside in, eg the Partials root folder.

    regards

    Marc

  • Colm Garvey 40 posts 65 karma points
    Nov 16, 2016 @ 16:44
    Colm Garvey
    0

    Thanks Marc,

    That did the trick!

    On a similar note.. could you explain why this isn't working?

    { Html.RenderPartial("~/Views/Partials/panels/" + @CurrentPage.title + ".cshtml");  }
    

    Thanks,

    Colm

Please Sign in or register to post replies

Write your reply to:

Draft