ASP.Net MVC PartialView的应用
1.PartialViewResult,Controller类提供了4种重载方法
protected internal PartialViewResult PartialView();
protected internal PartialViewResult PartialView(object model);
protected internal virtual PartialViewResult PartialView(string viewName, object model);
protected internal PartialViewResult PartialView(string viewName);
2.PartialView 一般最前加“_”后面加上Partial以便区分,例如实例中“_DetailPartial”
3.引用一个PartialView到一个视图页面里面,有几种方法,并且比较如下:
(此段落引用 _Cassie的 Asp.net mvc partialView文章)
1、Partial() Action()
2、RenderPartial()
3、RenderAction()
4、RenderPage()
这些方法有什么差别呢,分析他们的差别,可以让我们在不同的时候选择不同的方法。
*第一组:Partial与RenderPartial 方法*
Razor 语法:@Html.Partial() 与 @{Html.RenderPartial();}
Partial 可以直接输出内容,它内部是 将 html 内容转换为 string 字符(MVCHtmlString),然后缓存起
来,最后在一次性输出到页面。显然,这个转换的过程,会降低效率,所以通常使用 RenderPartial 代
替。
*第二组:RenderPartial 与 RenderAction 方法*
Razor 语法:@{Html.RenderPartial();} 与 @{Html.RenderAction();}
RenderPartial 不需要创建 Controller 的 Action ,而 RenderAction 需要在 Controller 创建要加载的
Action。
RenderAction 会先去调用 Contorller的 Action ,最后再呈现视图,所以这里 页面会在 发起一个链接。
如果这个部分视图只是一些简单 的 html 代码,请使用 RenderPartial。 但如果这个部分视图 除了有
html 代码外,还需要 通过 读取数据库里的数据 来渲染,就必须使用 RenderAction 了,因为 它可以在
Action 里调用 Model里的方法读取数据库,渲染视图后在呈现,而 RenderPartial 没有 Action,
所以无法做到。
*第三组:RenderAction 与 Action*
Razor 语法:@{Html.RenderAction();} 与@Html.Action();
Action 也是直接输出,和 Partial 一样,也存在一个转换的过程。不如 RenderAction 直接输出到 当前
HttpContext 的效率高。
*第四组:RenderPage 与 RenderPartial 方法*
Razor 语法:@{Html.RenderPartial();} 与@RenderPage()
也可以使用 RenderPage 来呈现部分,但它不能使用 原来视图的 Model 和 ViewData ,只能通过参数来
传递。而RenderPartial 可以使用原来视图的 Model 和ViewData。
4.索引PartialView方式
@Html.Partial(“ViewName”) 首先在当前目录下搜索ViewName,如果没有找到,再去Share目录查找ViewName名字。如果这两个目录下都没有则会报错。
@Html.Partial(“ViewName.cshtml”) View必须在相同目录下(根据文件全名称调用)。
@Html.Partial("~/Views/Folder/ViewName.cshtml")根据应用程序的根目录去查找。
@Html.Partial("…/Folder/ViewName.cshtml") 使用相对路径去定位。
5.实例展示
Controller:
public ActionResult Index()
{
return View(db.UserInfo.OrderBy(o=>o.ID).ToList());
}
public ActionResult Detail(string name)
{
if (name == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
else
{
var query = db.UserInfo.Find(name.Trim());
return PartialView("_DetailPartial", query);
}
}
Index:
注意1.ajax返回值类型为html,并InnerHtml到指定div中
注意2.获取行内主键值,作为Click事件的参数传入后台
@using WebApplication21.Models;
@model IEnumerable<UserInfo>
<html>
<head>
<script type="text/javascript" src="~/Scripts/jquery-3.4.1.js"></script>
</head>
<body>
<h2>This is an Index</h2>
<h4>UserInfo</h4>
<hr/>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach(var item in Model)
{
<tr>
<td>@item.ID</td>
<td>@item.Name</td>
<td>@item.Age</td>
<td>@item.Gender</td>
<td>
<button class="btn btn-success" onclick="GetDetail('@item.Name')">Detail</button>
<a href="/UserInfo/[email protected]" class="btn btn-danger"> Edit </a>
</td>
</tr>
}
</tbody>
</table>
<hr/>
<div id="detail"></div>
<script type="text/javascript">
function GetDetail(name) {
$.ajax({
url: "/UserInfo/Detail",
type: "GET",
dataType: "html",
data: {
"Name": name },
success: function (data) {
$("#detail").html(data);
}
});
};
</script>
</body>
</html>
_DetailPartial:
@using WebApplication21.Models;
@model UserInfo
<html>
<head>
<script type="text/javascript" src="~/Scripts/jquery-3.4.1.js"></script>
</head>
<body>
<br/>
<hr/>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>Phone</th>
<th>LastUpdateTime</th>
</tr>
</thead>
<tbody>
<tr>
<td>@Model.Name</td>
<td>@Model.Address</td>
<td>@Model.Phone</td>
<td>@Model.LastUpdateTime</td>
</tr>
</tbody>
</table>
</body>
</html>