MVC中的部分视图 [英] Partial views in MVC

查看:89
本文介绍了MVC中的部分视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个演示项目,其中我使用实体数据从数据库动态渲染菜单模型我已将模型添加到我的模型文件夹中,在此模型中我有我的菜单表(id,parentId,title)

然后我做了一个这样的菜单控制器

I have a demo project in which i have rendered a menu dynamically from database using Entity data Model I have Add a Model into my Models Folder and in this model i have my menu table (id,parentId,title)
then i have made a menu controller like that

private TestEntities testEntities = new TestEntities();
public ActionResult Index()
{
ViewBag.MenuLevel1 = this.testEntities.Menus.Where(menu=>menu.ParentId==null ).ToList();
return View();
}





和我的视图--->菜单--->索引我有那个代码



and in my View--->Menu--->index i have that code

@Model Menu.ModelTest
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<ul>
@foreach (var menuLevel1 in ViewBag.MenuLevel1)
{
<li>@menuLevel1.Name
@if (menuLevel1.Child.Count > 0)
{
<ul>
@foreach (var menuLevel2 in menuLevel1.Child)
{
<li>
@menuLevel2.Name
</li>
}
</ul>
}
</li>
}
</ul>
</div>
</body>
</html>



它的工作正常并做得很好,现在我想创建一个部分视图,因为我希望这个菜单被调用或放置任何时间如何做到这一点。



基本上我是MVC的新手我希望布局文件就像asp.net中的母版页一样,我希望菜单在这个布局文件中是动态的。任何帮助请,我知道部分视图,但部分视图是动态我的意思是显示来自数据库的数据和控制器操作方法以及如何调用



我尝试了什么:



我试图从菜单表中创建一个局部视图强类型,然后是html.Partial(ViewName )它不起作用


its working fine and did the good job , Now i want to create a Partial View of that because i want this menu to be called or placed any where any time how to do that .

Basically i am newby in MVC i want the layout file just like a master page in asp.net and i want the menu to be dynamic in this layout file. any help please , I know about partial views but a partial view which is dynamic i mean which shows data from database and having controller action method as well how to call that

What I have tried:

I have tried to create a partial view strongly type from menu table and then html.Partial("ViewName") it did not work

推荐答案

我不确定你是否真的有部分视图,或者你正在使用现有的视图渲染为局部视图。 br />
渲染局部视图的Razor语法是

I am not sure if you really have a partial view or you are using the existing "View" to be rendered as partial view.
The Razor syntax to render a partial view is
@Html.Partial("PartialView")  






or

@{Html.RenderPartial("PartialView");} 



我认为你正在创建一个视图并在渲染局部视图时渲染它。部分视图是一个特殊的可重用视图,它位于共享文件夹中。创建视图时,您必须选中创建为部分视图复选框。



检查此链接 [ ^ ],用于创建和渲染局部视图的完整过程。



希望它有所帮助。


I think you are creating a "View" and rendering it as you render a "Partial View". A partial view is a special reusable view and it is placed inside "Shared" folder. When you create a view you will have to check "Create as Partial View" checkbox.

Check this link[^] for a complete process of creating and rendering partial view.

Hope it helps.


这篇关于MVC中的部分视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆