多个部分视图。 Id碰撞

时间:2014-09-06 06:14:30

标签: asp.net-mvc-4 razor partial-views

我在项目中使用DataTables,我想在一个页面中放置两个相似的列表。我已经决定对两者使用相同的局部视图,但是当我尝试将DataTables功能应用于它们时,我遇到了Id的冲突。

我的部分观点:

<table style=" margin:0" id="incidents">
...
</table
<script type="text/javascript">
$('#incidents').DataTable();

我的父母观点:

<h2>Important incidents</h2>
@Html.Partial("Incidents", Model.Where(m => m.Relevance == true)
<h2>Other incidents</h2>
@Html.Partial("Incidents", Model.Where(m => m.Relevance == false).Where(m=>m.Date>DateTime.UtcNow.AddHours(-6).AddDays(-5)))

是否可以在不创建两个类似的部分视图的情况下执行此操作?

1 个答案:

答案 0 :(得分:1)

您可以通过使用id

将值从主视图传递到部分来为表提供不同的ViewDataDictionary属性

主视图

<h2>Important incidents</h2>
@Html.Partial("Incidents", Model.Where(...), new ViewDataDictionary { { "id", "important" } })
<h2>Other incidents</h2>
@Html.Partial("Incidents", Model.Where(...), new ViewDataDictionary { { "id", "other" } })

<script type="text/javascript">
  $('#important').DataTable();
  $('#other').DataTable();

部分

<table id="@ViewData["id"]">
  ....
</table>
相关问题