【问题标题】:How to use a subview with a different model than that of the main view?如何使用与主视图不同模型的子视图?
【发布时间】:2011-05-18 21:04:23
【问题描述】:

我想使用局部视图来表示表格的行:

// SomeItem.cshtml
@model SomeItemModel
<tr>
    <td>
        @Html.DisplayTextFor(m => m.SomeField)
    </td>
    <td>
        @Html.DisplayTextFor(m => m.AnotherField)
    </td>
</tr>

并从表示包含该表的页面的视图中调用它:

// SomeCollection.cshtml
@model SomeCollection
...
<table><tbody>
@foreach (TipoDocumentoModel item in Model) {
    Html.Partial("SomeItem", item);
}>
</tbody></table>
...

但它不起作用。为什么?

【问题讨论】:

    标签: c# razor partial-views


    【解决方案1】:

    调用Html.Partial 会返回MvcHtmlString,而不向页面写入任何内容。

    您需要添加@ 以将MvcHtmlString 呈现到页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 2016-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      • 1970-01-01
      相关资源
      最近更新 更多