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/...." ?
-
+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.codeandcloud– codeandcloud2014-02-27 06:33:47 +00:00Commented 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 itSuperCuke– SuperCuke2014-02-27 06:37:32 +00:00Commented Feb 27, 2014 at 6:37
Add a comment
|
1 Answer
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)
}