Html.Action与Html.RenderAction的用法与区别:RenderAction要写在代码中!!!1
https://blog.csdn.net/sym218/article/details/38681531
Action是执行单独的控制器并且显示结果,Action与RenderAction不同的是,Action返回的是字符串,而RenderAction是写入响应流,因此RenderAction是要写在代码中:
@Html.Action("test")
@{
Html.RenderAction("test");
}
下面来介绍怎样具体实现Html.Action:
1.首先在Models里面创建两个类:Menu.cs 和MenuItem.cs
public class Menu
{
public List<MenuItem> Items { get; set; }
}
public class MenuItem
{
public string Text { get; set; }
public string Url { get; set; }
}
2.在Controllers添加代码:
[ChildActionOnly]
public ActionResult Menus()
{
Menu menu = new Menu();
List<MenuItem> items = new List<MenuItem>();
items.Add(new Models.MenuItem() { Text = "Baidu", Url = "http://www.baidu.com" });
items.Add(new Models.MenuItem() { Text = "Sina", Url = "http://www.sina.com" });
items.Add(new Models.MenuItem() { Text = "Sohu", Url = "http://www.sohu.com" });
items.Add(new Models.MenuItem() { Text = "IBM", Url = "http://www.ibm.com" });
menu.Items = items;
return PartialView(menu);
}
其中[ChildActionOnly]这个特性设置了防止运行时直接通过一个URL调用Menu操作,只能通过Action和RenderAction操作。这个特性不是必须要用的。
3.创建Menus视图:
@model Example.Models.Menu
<ul>
@foreach (var item in Model.Items)
{
<li><a href="@item.Url">@item.Text</a></li>
}
</ul>
4.在将要调用@Html.Action的视图中添加本例在Index.cshtml:
<div>
@Html.Action("MyMenu")
<hr />
@{
Html.RenderAction("MyMenu");
}
</div>
这是两种方式的调用。
__________________________________________________ ____________________________ ____________________________ ____________________________ _______________________________
https://yq.aliyun.com/articles/499866/
__________________________________________________ ____________________________ ____________________________ ____________________________ _______________________________