我在
我该如何传递当前的
ViewData.Model
中有一些数据,我想编写一个局部视图,并传递当前页面上的模型。我该如何传递当前的
ViewData.Model
并通过局部视图的位置渲染它们?ViewData.Model
中有一些数据,我想编写一个局部视图,并传递当前页面上的模型。ViewData.Model
并通过局部视图的位置渲染它们?创建你的局部视图,例如:
@model YourModelType
<div>
<!-- HTML to render your object -->
</div>
然后在你的视图中使用:
@Html.Partial("YourPartialViewName", Model)
如果您不想要强类型的局部视图,请从局部视图顶部移除@model YourModelType
,它将默认为dynamic
类型。
更新:
默认视图引擎将在调用局部视图的视图所在文件夹和~/Views/Shared文件夹中搜索局部视图。如果您的局部视图位于不同的文件夹中,则需要使用完整路径。请注意以下路径中使用了~/
。@Html.Partial("~/Views/Partials/SeachResult.cshtml", Model)
<%= Html.Partial("PartialName", Model) %>
@Html.RenderPartial(string viewName)
是用来做什么的?它总是会报错,提示Cannot implicitly convert type 'void' to 'object'
。 - shashwat