asp .net core 部分视图
一、创建
1、位置:Shared 文件夹
2、视图名:_xxxxPartialView.cshtml
二、使用
1、引用
<partial name = ""/> 需要引用 addTageHelper 内容 @await Html.PartialAsync("") 内容 @await Html.RenderPartialAsync("") stream
a、引入
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
使用 model
@{ ListData newListData = new ListData { ListTitle = "语言", ListItems = new List<string> { "Java", "C#", "Python", "Php" } }; } @* @await Html.PartialAsync("_ListPartialView", newListData) *@ <partial name="_ListPartialView" , model="newListData" />
推荐第二个,不用额外引用,第三个效率高
2、ViewData/ViewBag
3、Model
4、部分视图
return PartialView(listData);
注意:ViewData数据是备份的
案例
部分视图接收数据 ViewBag.
_PersonSortedPartialView.cshtml
@if (ViewBag.SortBy == ViewBag.ColunmName && ViewBag.SortOrder == "Asc") { <th><a href="~/persons/index?searchBy=@(ViewBag.searchBy)&searchStr=@(ViewBag.SearchStr)&sortBy=@ViewBag.ColunmName&sortOrder=Desc">@ViewBag.DisplayName</a> </th> } else if (ViewBag.SortBy == ViewBag.ColunmName && ViewBag.SortOrder == "Desc") { <th><a href="~/persons/index?searchBy=@(ViewBag.searchBy)&searchStr=@(ViewBag.SearchStr)&sortBy=@ViewBag.ColunmName&sortOrder=Asc">@ViewBag.DisplayName</a> </th> } else { <th><a href="~/persons/index?searchBy=@(ViewBag.searchBy)&searchStr=@(ViewBag.SearchStr)&sortBy=@ViewBag.ColunmName&sortOrder=Asc">@ViewBag.DisplayName</a> </th> }
向部分视图传输数据
@await Html.PartialAsync("_PersonSortedPartialView", new ViewDataDictionary(ViewData){ {"ColunmName", nameof(PersonResponse.PersonName)}, {"DisplayName", "Person Name" } })

浙公网安备 33010602011771号