2
可重複使用的內容部分我有HTML以下塊顯示的東西,比如評論:創建在MVC
<div class="comment">
...
</div>
而這個HTML塊使用註釋對象來顯示數據。
我正在使用剃刀。
如何創建這個部分,所以我可以在我的其他視圖頁面重新使用它,通過傳遞在評論對象。
這是局部視圖嗎?
可重複使用的內容部分我有HTML以下塊顯示的東西,比如評論:創建在MVC
<div class="comment">
...
</div>
而這個HTML塊使用註釋對象來顯示數據。
我正在使用剃刀。
如何創建這個部分,所以我可以在我的其他視圖頁面重新使用它,通過傳遞在評論對象。
這是局部視圖嗎?
這是局部視圖嗎?
是的,這看起來像一個很好的候選人的局部視圖(~/Views/Shared/_Comment.cshtml
):
@model CommentViewModel
<div class="comment">
...
</div>
,然後當你需要的地方使用它:
@model SomeViewModel
...
@Html.Partial("_Comment", Model.Comment)
另一種可能性是使用顯示模板(~/Views/Shared/DisplayTemplates/CommentViewModel.cshtml
):
@model CommentViewModel
<div class="comment">
...
</div>
,然後當你需要的地方使用它:
@model SomeViewModel
...
@Html.DisplayFor(x => x.Comment) // the Comment property is of type CommentViewModel
,但另一種可能是使用Html.Action and Html.RenderAction幫手。
因此,您可以看到ASP.NET MVC提供了創建可重用部件的不同方式。
vs.net似乎不以下劃線前綴創建部分剃刀頁面時? – Blankman
Underscore是Razor防止直接加載html頁面的慣例。用下劃線不能直接到達。 –