0

I have an MVC Application broken down into multiple areas. The structure is like this: Areas > AreaName > Views > various view folders

I need to add a folder inside these view folders called HelpFiles making the structure like this Areas > AreaName > Views > various view folders > HelpFiles

In my Layout page, I have an icon when its clicked, it will call a controller method that will return all partial views inside the HelpFiles folder and display them inside a div.

What I have done so far: I have added a method to the basecontroller like this:

   public ActionResult ShowHelpText(string viewName)
        {
            var areaName = ControllerContext.RouteData.DataTokens["area"];
            var folderName = "~"+ areaName + viewName + "HelpFiles/";
            ViewBag.Partials = ????
            return View();
        }

On the returning view my code is like this:

@foreach (string partial in ViewBag.Partials)
{
    //Html.RenderPartial(partial);
    @Html.Partial(partial)
}

My problem is I am not able to figure a way to get all the views from a folder.

I realize this is not the ideal way of doing this, the help text should come from a database etc, however due to a CMS service tie in, we are stuck with this structure for now.

Any help or pointers will be much appreciated.

Thanks

2
  • 1
    This is not the best location to put your help files. Put them in the Content folder. if not exist, then create a folder named Content et put your HelpFiles sub-folder. Commented Feb 22, 2016 at 21:24
  • Yes you are right, this is not the ideal way to do this, however this is not a choice thats left on me. Commented Feb 22, 2016 at 22:25

1 Answer 1

1

Untested code, you might need to make some little changes in order to make it work.

//Replace the `~/Content/HelpFiles` to the real path.
ViewBag.Partials = Directory.EnumerateFiles(Server.MapPath("~/Content/HelpFiles")).Select(i => Path.GetFileName(i));

View:

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

1 Comment

That is the way to go. Thank you. My finished code was like this: ViewBag.Partials = Directory.EnumerateFiles(HostingEnvironment.MapPath(folderName)).Select(i => Path.GetFileName(i)); ViewBag.Path = folderName; return View("~/areas/common/views/base/showhelptext.cshtml"); @foreach (string partial in (IEnumerable<string>)ViewBag.Partials) { string path = ViewBag.Path + partial; @Html.Partial(path) }

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.