【问题标题】:How to render partial again and again(only with RenderAction)如何一次又一次地渲染局部(仅使用 RenderAction)
【发布时间】:2011-12-22 08:24:52
【问题描述】:

如何一次又一次地渲染部分(使用Html.RenderPartial())。

或如何使用html.Action("","") 一次又一次地渲染局部(通过调用返回局部视图的操作)。

【问题讨论】:

  • Html.ActionLink(...) 与渲染Partial 或Action 无关。你的意思是Html.Action(...) 还是Html.RenderAction(...)?无论如何,只需创建一个循环并将您的渲染助手放入其中,例如@satish kumar answer。

标签: asp.net-mvc html-helper html.actionlink


【解决方案1】:

据我了解,您需要在页面上多次显示 ParialView。如果这是你的问题,那么你有一些东西,比如一个集合,它告诉你想在页面上呈现多少次部分视图。
例如
for(int i =0; i {
@Html.Partial("")
}
这将显示 5 次局部视图。

【讨论】:

    【解决方案2】:

    您可以使用 jQuery 的 $.get() 或 $.ajax()。

    放置一些 html 元素 (<div class='myclass'>)

    <div id='container>
      <div class='myclass' my-ajax-url='@Model.urlForWhatever' my-url-param='@Model.foo'>
      </div>
    
      <div class='myclass' my-ajax-url='@Model.urlForWhatever2' my-url-param='@Model.bar'>
      </div>
    
    </div>
    

    添加类似:

    $(document).ready(function () {
      var elems = $('.myclass').each(function () {
         // Perform Ajax call and render the output (contained in response)
         // the element is referreable using $(this) in the each function, so you can swap
         // its content via $(this).html().
         var url=$(this).attr('my-ajax-url') + '&param=' + $(this).attr('my-url-param');
         $.get(url, function(data) {
             $(this).html(data);
         });
      });
    });
    

    Ajax 助手很好,但我发现它对于某些东西有些限制。 我更喜欢将数据存储在 DOM 元素中,然后使用 jQuery 来构建 url(那段代码被简化了,没有人会在不先检查的情况下添加 &name=value)

    PS:写这个没有机会测试它,但至少应该给出一个关于如何做到这一点的想法。

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多