1

i am having my views separated under subfolder, i have only one Action method in controller, i have my view names in the database, when i call the action method by passing the parameter (for instance: id = 1), it will fetch the view name from the database and the respective view will be load.

public ActionResult Index(int FormId)
 {
   var getViews = db.fetchViews.where id = 1; //get views from db
   return view(getviews.viewName);
 }

This is my views in the solution.

enter image description here

while i call the view from action method it says unable to find the views.

i cannot hardcode the subfolder in the Action method like this,

return View("~/Views/Form/Customer1/getviews.viewName");

any ideas would be appreciate...

3 Answers3

0

Like this?

public ActionResult Index(int FormId)
{
    var viewName = "~/Views/Form/Customer" + FormId + "/customer" + FormId;
    return View(viewName);
 }
Backs
  • 24,430
  • 5
  • 58
  • 85
0

If you can't move the views to the directory that the razor view engine is looking for, you can try this:

public ActionResult Index(int FormId)
{
    var getViews = db.fetchViews.where id = 1; //get views from db
    var viewpath = string.Format("~/Views/Form/{0}/{0}", getviews.viewName);
    return View(viewpath);
}

Alternatively, you could create a custom RazorViewEngine that could search for the view in a subdirectory of the same name.

This post has a method of adding search view locations to the razor view engine, your custom search view format would be something like this:

new string[] 
{ 
    "~/Views/{1}/{0}/{0}.cshtml"
}
Community
  • 1
  • 1
Travis Schettler
  • 844
  • 1
  • 6
  • 10
  • I am not aware of RazorViewEngine that search for view, so can i have any alternatives? –  Feb 02 '16 at 07:40
  • I have updated my answer with an SO post that gives a method to add custom view locations to the existing razor view engine. – Travis Schettler Feb 02 '16 at 16:44
  • i didn't use this Razor Engine search rather i would go with your string format method which is more simple... –  Feb 04 '16 at 09:42
0

If you want the searching of views to be according to what you want, you can inherit the RazorViewEngine and modify the path where Razor will look at. You may see this SO post

Community
  • 1
  • 1
Bon Macalindong
  • 1,310
  • 13
  • 20