考虑一个呈现分页导航的 ViewComponent。传递给组件的参数之一是当前 PageIndex(查看页面的索引)。
如何在我的 ViewComponents 模板中使用这个传递的值而不将它放入 ViewData?
-- 视图组件:
[ComponentName("GalleryPager")]
public class PagerComponent : ViewComponent
{
[Parameter("PageIndex")]
private int _pageIndex;
public void Run()
{
... some logic ...
ViewData["PageIndex"] = _pageIndex; // I want to avoid this!
}
}
-- 父视图:
...
<span class="paging">
{% GalleryPager @PageIndex = SOME_VALUE %}
</span>
...
-- 组件视图:
...
<span class="page_index">
{{ PageIndex }} <!-- This way it's passed/used as ViewData -->
</span>
...