RenderPartial和RenderAction区别

来源:互联网 发布:java解析ini文件 编辑:程序博客网 时间:2024/05/04 13:38

本篇参考了 Shailendra Chauhan和 Jag Reehal的博文。


RenderParital和RenderAction的共同点:

※ 都能返回部分视图

※ 返回的部分视图和主视图共用一个TextWriter对象把内容写到Http Response中


  什么时候使用Html.RenderPartial


当部分视图中的数据是主视图的一部分,即部分视图的Model是主视图Model的一个集合类型属性的时候,倾向于用Html.RenderPartial。


就像如下为了展示主视图view model的一个集合类型属性IEnumerable<Comment> Comments,就把这里的Comment作为部分视图的Model。


□ domain model


    public class Post

    {

        public string Title { get; set; }

        public string Content { get; set; }

        public DateTime DatePublished { get; set; } 

    }


    public class Comment

    {

        public string Content { get; set; }

        public string Author { get; set; }

        public string ImageUrl { get; set; }

        public DateTime DateCreated { get; set; } 

    }


□ view model


    public class ShowPostViewModel

    {

        public Post Post { get; set; }

        public IEnumerable<Comment> Comments { get; set; } 

    }


□ Controller


        public ActionResult Index()

        {

            //view model

            ShowPostViewModel viewModel = new ShowPostViewModel();

            viewModel.Post = new Post

            {

                Title = "今晚广州恒大将开始2014亚冠联赛之旅",

                Content = "创业难守业更难,广州恒大能否卫冕成功,让我们拭目以待!",

                DatePublished = new DateTime(2014,2,26)

            };


            viewModel.Comments = new List<Comment>

            {

                new Comment()

                {

                    Author = "广州球迷",

                    Content = "广州未赢够",

                    DateCreated = new DateTime(2014,2,26)

                },

                new Comment()

                {

                    Author = "山东球迷",

                    Content = "今天看我们大鲁能",

                    DateCreated = new DateTime(2014,2,26)

                }

            };

            return View(viewModel);

        }    


□ _Comments部分视图


@model IEnumerable<RenderPartialAndRenderAction.Models.Comment>


@foreach (var comment in Model)

{

    @comment.Author @:在  @comment.DateCreated.ToLongDateString() 发表评论:

    @comment.Content

    <hr/>                   

}                


□ Index视图


@model RenderPartialAndRenderAction.Models.ShowPostViewModel

    <div>

        <h1>@Model.Post.Title</h1>

        提交日期 @Model.Post.DatePublished.ToLongDateString() <br/>

        @Model.Post.Content <br/>

        评论数: @Model.Comments.Count()

        <hr/>

        @{Html.RenderPartial("_Comments",Model.Comments);}

    </div>      


□ 结果



  什么时候用Html.RenderAction


当部分视图的数据,相对来说,与主视图的关系比较独立,或者在多个主视图页面被用到(比如,可以放到_Layout.cshtml中),用Html.RenderAction比较适合。


就像如下的AnotherViewModel与如上的ShowPostViewModel,相对关系不大。我们就通过或Html.RenderAction来获取有关AnotherViewModel的部分视图。


□ domain model


    public class Category

    {

        public int Id { get; set; }

        public string Name { get; set; } 

    }     


□ view model 增加一个用于显示类别的view model


    public class AnotherViewModel

    {

        public string StringToOutput { get; set; }

        public IEnumerable<Category> Categories { get; set; } 

    }      


□ Controller,RenderAction("action方法","controller名"),这里的action方法必须打上[ChildActionOnly]


        [ChildActionOnly]

        public ActionResult ShowCate()

        {

            var viewModel = new AnotherViewModel

            {

                StringToOutput = "所有类别",

                Categories = new List<Category>

                {

                    new Category()

                    {

                        Name = "类别1"

                    },

                    new Category()

                    {

                        Name = "类别2"

                    }

                }

            };

            return PartialView(viewModel);

        }       


□ 视图


    <div>

        <h1>@Model.Post.Title</h1>

        

        提交日期 @Model.Post.DatePublished.ToLongDateString() <br/>

        @Model.Post.Content <br/>

        评论数: @Model.Comments.Count()

        <hr/>

        @{Html.RenderPartial("_Comments",Model.Comments);}

        <h5>@{Html.RenderAction("ShowCate");}</h5>

    </div>


□ 效果

    

     

  Html.Partial和Html.Action


Html.Partial和Html.Action相同之处:

※ 返回值都是HtmlString

※ 返回值都都可以赋值给变量


Html.Partial和Html.Action不同之处:

※ 与Html.RenderPartial和Html.RenderAction类似。


0 0
原创粉丝点击