url in MVC project of an . html

Asked

Viewed 120 times

0

would like to create a . html that will serve to be the menu of _Layout.cshtml, I will call it through $("#IDMenu").load('menu_Home.html');

My question, where better to leave it? I can leave it in the case

/Shared/ ?
_Layout.cshtml
menu_home.html
menu_admin.html

and on this masterpage _Layout.cshtml, as he would call it?

/views/Shared/menu_home.html `não funcionou`
Shared/menu_home.html `não funcionou` 
  • It is really necessary to use the load() jQuery to do this or it can be done otherwise?

  • Is there any reason why this is not a partial view?

  • could be, I’ll read about it. is a static content.

  • I would leave it on Shared, because it can serve for other pages.

  • Thiago.adriano26 wants to know which error happened. It would be useful for everyone to help you better.

  • No mistake, but call a page. html via jquery.load da as 404, anyway, the url is not accessible I think because I don’t have a controller and this inside the views folder

Show 1 more comment

1 answer

2


If static, use in your file _Layout.cshtml:

@Html.Partial("_Menu")

Create a file for this case _Menu.cshtml

If dynamic, use a Action:

@Html.Action("Menu")

Create for this case a Controller common and put in it a Action that populates this menu:

namespace MeuProjeto.Controllers
{
    public class CommonController : Controller
    {
        private MeuProjetoContext context = new MeuProjetoContext();

        [ChildActionOnly]
        public ActionResult Menu()
        {
            var menu = context.EntradasDoMenu.ToList();

            return PartialView(menu);
        }
    }
}

Also create a View called Shared\Menu.cshtml that receives a model of the type IEnumerable<EntradaDoMenu> (I used this name in my example, but you can create a Model with the name you want):

@model IEnumerable<MeuProjeto.Models.EntradaDoMenu>

<ul>
    @foreach (var entrada in Model) { 
       <li>@Html.ActionLink(entrada.Nome, "Index", entrada.NomeDoController)</li>
    }
</ul>

Browser other questions tagged

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