3

I have a folder that contains about 20 partial views without any model (just plain javascript/html). Is there a simple way to render all views from a specific folder, for example "/Views/Shared/Forms/...." ?

2
  • +1: interesting question. have you tried listing all files in that folder to list<string> and writing a for loop? i dont know if you have permission to do so actually? just a thought. Commented Feb 27, 2014 at 6:33
  • Thought about that, but it will require reading permissions, than passing all view names from controller to a view - hoped to find a simpler way to do it Commented Feb 27, 2014 at 6:37

1 Answer 1

10

I don't think there is another method to render all Partials from a certain folder to a View. However you can do this. (Tested)

Controller

public ActionResult Index()
{
    var folderName = "~/Views/Partials/";
    ViewBag.Partials = GetAllViews(folderName);
    return View();
}

private List<string> GetAllViews(string folderName)
{
    var path = Server.MapPath(folderName);
    var dirInfo = new DirectoryInfo(path);
    return dirInfo.GetFiles().Select(i => folderName + i.Name).ToList();
}

View

@foreach (string partial in ViewBag.Partials)
{
    //Html.RenderPartial(partial);
    @Html.Partial(partial)
}
Sign up to request clarification or add additional context in comments.

Comments

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.