MVC分部视图的使用:Html.Partial/RenderPartial,Html.Action/RenderAction,RenderPage

ASP.NET MVC 里的部分视图,相当于 Web Form 里的 User Control。我们的页面往往会有许多重用的地方,可以进行封装重用。
使用部分视图有以下优点: 1. 可以简写代码。 2. 页面代码更加清晰、更好维护。
在视图里有多种方法可以 加载部分视图,包括: Partial() 、RenderPartial() 、 Action() 、RenderAction() 、 RenderPage() 方法

一、Partial与RenderPartial

1.Razor 语法: @Html.Partial() 与 @{Html.RenderPartial();}
2.区别:Partial 可以直接输出内容,它内部是 将 html 内容转换为 string 字符(MVCHtmlString)(进行Html编码),然后缓存起来,最后在一次性输出到页面。显然,这个转换的过程,会降低效率,所以通常使用 RenderPartial 代替。 这两者都只是抓取分部视图页面类容,不能执行分部视图方法,所以用Partial或RenderPartial方法来显示分部视图不用建立对应的Action,因为不走Action.
3.实例:

普通调用分部视图

主页 Index.cshtml:

@{  
    Layout = null;  
}  
  
<!DOCTYPE html>  
  
<html>  
<head>  
    <meta name="viewport" content="width=device-width" />  
    <title>Index</title>  
</head>  
<body>  
    <div>   
        <h3>我是首页</h3>  
        <section>  
            <h2>分部视图</h2>  
            @Html.Partial("~/Views/Templates/Partial1.cshtml")  
        //@{Html.RenderPartial("~/Views/Templates/Partial1.cshtml");}  
 </section> </div></body></html>  
分部视图Partial1.cshtml:  
<table border="1px solid" cellpadding="0" cellspacing="0">  
    <tr>  
        <th>姓名</th>  
        <th>性别</th>  
        <th>年龄</th>  
        <th>电话</th>  
    </tr>  
    <tr>  
        <td>longxi1</td>  
        <td></td>  
        <td>22</td>  
        <td>13521187063</td>  
    </tr>  
    <tr>  
        <td>longxi1</td>  
        <td></td>  
        <td>22</td>  
        <td>13521187063</td>  
    </tr>  
</table>  
强类型分部视图:  
主页 Index.cshtml:  
@using WebApplication1.Models  
@{  
    Layout = null;  
}  
@{   
    List<Student> students = new List<Student>() {  
        new Student("zhulongxi",22,"男","13521187063"),  
        new Student("zhulongxi",22,"男","13521187063"),  
        new Student("zhulongxi",22,"男","13521187063"),  
        new Student("zhulongxi",22,"男","13521187063"),  
        new Student("zhulongxi",22,"男","13521187063")  
    };  
}  
<!DOCTYPE html>  
  
<html>  
<head>  
    <meta name="viewport" content="width=device-width" />  
    <title>Index</title>  
</head>  
<body>  
    <div>   
        <h3>我是首页</h3>  
        <section>  
            <h4>分部视图</h4>  
            @Html.Partial("~/Views/Templates/Partial1.cshtml", students)//如果Partial1.cshtml与Index.cshtml在相同目录,则可以直接写成  
        @Html.Partial("Partial1", students)  
 </section> </div></body></html>  

分部视图Partial1.cshtml:

@using WebApplication1.Models;  
@{   
    var studentsList = Model as List<Student>;  
}  
<table border="1px solid" cellpadding="0" cellspacing="0">  
    @foreach (Student student in studentsList)  
    {  
        <tr>  
            <th>@student.Name</th>  
            <th>@student.Gender</th>  
            <th>@student.Age</th>  
            <th>@student.Phone</th>  
        </tr>  
    }  
</table>  

二、Action与RenderAction

1.Razor 语法:@Html.Action()与@{Html.RenderAction();}
2.区别:Action 也是直接输出,和 Partial 一样,也存在一个转换的过程。不如 RenderAction 直接输出到当前 HttpContext 的效率高。
除此之外,Action与Partial相比,Action访问了控制器中的Action,执行了Action内部的业务。
3.实例:
Index.cshtml:

<!DOCTYPE html>  
  
<html>  
<head>  
    <meta name="viewport" content="width=device-width" />  
    <title>Index</title>  
</head>  
<body>  
    <div>   
        <h3>我是首页</h3>  
        <section>  
            <h4>分部视图</h4>  
            @Html.Action("MyPartial", "Home",new { title="学生列表"})  
        </section>  
    </div>  
</body>  
</html>  

HomController:

public class HomeController : Controller  
    {  
        // GET: Home  
        public ActionResult Index()  
        {  
            return View();  
        }  
        public ActionResult MyPartial(string title)  
        {  
            List<Student> students = new List<Student>() {  
            new Student("zhulongxi2",22,"男","13521187063"),  
            new Student("zhulongxi2",22,"男","13521187063"),  
            new Student("zhulongxi2",22,"男","13521187063"),  
            new Student("zhulongxi2",22,"男","13521187063"),  
            new Student("zhulongxi2",22,"男","13521187063")  
             };  
            ViewBag.Data = title;  
            return PartialView("~/Views/Templates/Partial2.cshtml",students);  
        }  
    }  

Partial2.cshtml:

@using WebApplication1.Models  
@{   
    var studentsList = Model as List<Student>;  
    var data = ViewBag.Data;  
}  
@{Response.Write(data); }  
<table border="1px solid" cellpadding="0" cellspacing="0">  
    @foreach (Student student in studentsList)  
    {  
        <tr>  
            <th>@student.Name</th>  
            <th>@student.Gender</th>  
            <th>@student.Age</th>  
            <th>@student.Phone</th>  
        </tr>  
    }  
</table>  

请添加图片描述

三、RenderPage

1.Razor语法:@RenderPage()
2.区别:也可以使用 RenderPage 来呈现部分,但它不能使用 原来视图的 Model 和 ViewData ,只能通过参数来传递。而 RenderPartial、RenderAction 可以使用原来视图的 Model 和 ViewData。@RenderPage也并没有执行Action。
3.实例:
不传参数情况:
Index.cshtml:

<!DOCTYPE html>  
  
<html>  
<head>  
    <meta name="viewport" content="width=device-width" />  
    <title>Index</title>  
</head>  
<body>  
    <div>   
        <h3>我是首页</h3>  
        <section>  
            <h4>分部视图</h4>  
            
           @RenderPage("~/Views/Templates/Partial1.cshtml")  
        </section>  
    </div>  
</body>  
</html>  

传参数情况:
Index.cshtml:

<!DOCTYPE html>  
<html>  
<head>  
    <meta name="viewport" content="width=device-width" />  
    <title>Index</title>  
</head>  
<body>  
    <div>   
        <h3>我是首页</h3>  
        <section>  
            <h4>分部视图</h4>  
           @RenderPage("~/Views/Templates/Partial1.cshtml",new { param1="longxi",param2="男"})  
        </section>  
    </div>  
</body>  
</html>  

Partial1.cshtml:

@{   
    var param = string.Format("{0}-{1}", PageData["param1"], PageData["param2"]);  
}  
@Html.Raw(param) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
W: http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://archive.ubuntu.com/ubuntu jammy InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://archive.ubuntu.com/ubuntu jammy InRelease' is not signed. W: http://security.ubuntu.com/ubuntu/dists/jammy-security/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://security.ubuntu.com/ubuntu/dists/jammy-security/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://security.ubuntu.com/ubuntu jammy-security InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://security.ubuntu.com/ubuntu jammy-security InRelease' is not signed. W: http://archive.ubuntu.com/ubuntu/dists/jammy-updates/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://archive.ubuntu.com/ubuntu/dists/jammy-updates/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://archive.ubuntu.com/ubuntu jammy-updates InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://archive.ubuntu.com/ubuntu jammy-updates InRelease' is not signed. W: http://archive.ubuntu.com/ubuntu/dists/jammy-backports/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://archive.ubuntu.com/ubuntu/dists/jammy-backports/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://archive.ubuntu.com/ubuntu jammy-backports InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://archive.ubuntu.com/ubuntu jammy-backports InRelease' is not signed. E: Problem executing scripts APT::Update::Post-Invoke 'rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true' E: Sub-process returned an error code
最新发布
06-07

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值