Return view that is in another directory

Asked

Viewed 831 times

2

I added a folder inside another folder in the views folder, this way:

Views/Register/Profile

Inside the profile folder are the files .cshtml. However, when I try to access the files .cshtml I get an error in the application, already tried this solution: How to direct an Action to a view inside a folder? and I couldn’t solve it, the AP says at the end, I have to create route, but I can’t.

My controller is as follows:

public ActionResult Perfil() 
{ 
    var perfils = db.Perfil.Where(p => p.Nome != "Administrador" && p.IDStatus == 1).ToList(); 
    return View("Perfil/"+perfils); 
} 

I get the following error: inserir a descrição da imagem aqui

  • 1

    Post the way you are doing on your controller. What error does it show?

  • No problem, at first it’s just like that. If you have something to add, you can always edit your own question. Do not use the answer field for this.

  • Welcome to [en.so], do a [tour], to learn a little more about the operation of the site to increase your chances of getting a good response.

1 answer

1


In this case, you don’t need to change the route to display your view, just return it in your action, this way:

public ActionResult Perfil() 
{ 
    var perfils = db.Perfil.Where(p => p.Nome != "Administrador" && p.IDStatus == 1).ToList(); 
    return View("Perfil/Perfil",perfils); 
} 

In this case, as you just want to return the view, but do not change the route, just return the view Profile which is inside the folder Profile.

  • Randrade sorry but I don’t think I was clear enough: My controller will call the Profile action, but this action is inside another folder with the profile name, like this: Views/Register/Profile/Profile.cshtml or Profile.cshtml is my action.

  • OK I can, I’ll do it now

Browser other questions tagged

You are not signed in. Login or sign up in order to post.