<small id='mvMLa'></small><noframes id='mvMLa'>

    • <bdo id='mvMLa'></bdo><ul id='mvMLa'></ul>

    1. <i id='mvMLa'><tr id='mvMLa'><dt id='mvMLa'><q id='mvMLa'><span id='mvMLa'><b id='mvMLa'><form id='mvMLa'><ins id='mvMLa'></ins><ul id='mvMLa'></ul><sub id='mvMLa'></sub></form><legend id='mvMLa'></legend><bdo id='mvMLa'><pre id='mvMLa'><center id='mvMLa'></center></pre></bdo></b><th id='mvMLa'></th></span></q></dt></tr></i><div id='mvMLa'><tfoot id='mvMLa'></tfoot><dl id='mvMLa'><fieldset id='mvMLa'></fieldset></dl></div>
      <tfoot id='mvMLa'></tfoot>
      <legend id='mvMLa'><style id='mvMLa'><dir id='mvMLa'><q id='mvMLa'></q></dir></style></legend>
    2. 用于 ajax 刷新的 Viewcomponent 替代方案

      时间:2023-08-08

    3. <legend id='p3kyn'><style id='p3kyn'><dir id='p3kyn'><q id='p3kyn'></q></dir></style></legend>

    4. <tfoot id='p3kyn'></tfoot>

        <bdo id='p3kyn'></bdo><ul id='p3kyn'></ul>
          <tbody id='p3kyn'></tbody>

              <i id='p3kyn'><tr id='p3kyn'><dt id='p3kyn'><q id='p3kyn'><span id='p3kyn'><b id='p3kyn'><form id='p3kyn'><ins id='p3kyn'></ins><ul id='p3kyn'></ul><sub id='p3kyn'></sub></form><legend id='p3kyn'></legend><bdo id='p3kyn'><pre id='p3kyn'><center id='p3kyn'></center></pre></bdo></b><th id='p3kyn'></th></span></q></dt></tr></i><div id='p3kyn'><tfoot id='p3kyn'></tfoot><dl id='p3kyn'><fieldset id='p3kyn'></fieldset></dl></div>

              1. <small id='p3kyn'></small><noframes id='p3kyn'>

              2. 本文介绍了用于 ajax 刷新的 Viewcomponent 替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个视图组件,其中包含一些嵌入到各个页面中的可重用业务逻辑.这一直运作良好.但是,我现在需要使用 ajax 刷新视图组件.

                I have a viewcomponent that contains some reusable business logic that embed in various pages. This has been working fine. However, I now have a requirement to refresh the viewcomponent using ajax.

                有没有办法做到这一点?根据我的阅读,这是不可能的,尽管该信息有点过时了.如果不可能,最好的选择是什么?

                Is there any way to accomplish this? From what I have read, it is not possible, although that info was a bit outdated. If it is not possible, what is the best alternative?

                推荐答案

                在 beta7 上,现在可以直接从控制器返回 ViewComponent.检查 公告

                On beta7 it is now possible to return a ViewComponent directly from a controller. Check the MVC/Razor section of the announcement

                MVC 中新增的 ViewComponentResult 使得返回结果变得容易来自动作的 ViewComponent.这使您可以轻松地暴露ViewComponent 作为独立端点的逻辑.

                The new ViewComponentResult in MVC makes it easy to return the result of a ViewComponent from an action. This allows you to easily expose the logic of a ViewComponent as a standalone endpoint.

                所以你可以有一个像这样的简单视图组件:

                So you could have a simple view component like this:

                [ViewComponent(Name = "MyViewComponent")]
                public class MyViewComponent : ViewComponent
                {
                    public IViewComponentResult Invoke()
                    {
                        var time = DateTime.Now.ToString("h:mm:ss");
                        return Content($"The current time is {time}");
                    }
                }
                

                在控制器中创建一个方法,例如:

                Create a method in a controller like:

                public IActionResult MyViewComponent()
                {
                    return ViewComponent("MyViewComponent");
                }
                

                并且比我快速而肮脏的 ajax 刷新做得更好:

                And do a better job than my quick and dirty ajax refresh:

                var container = $("#myComponentContainer");
                var refreshComponent = function () {
                    $.get("/Home/MyViewComponent", function (data) { container.html(data); });
                };
                
                $(function () { window.setInterval(refreshComponent, 1000); });
                

                当然,在 beta7 之前,您可以创建一个视图作为@eedam 建议的解决方法,或者使用 这些答案

                Of course, prior to beta7 you could create a view as the workaround suggested by @eedam or use the approach described in these answers

                这篇关于用于 ajax 刷新的 Viewcomponent 替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 Javascript 中获取文本框的值 下一篇:jQuery:与在不适当的时间执行的 removeData() 冲突

                相关文章

                最新文章

                <legend id='KdOkP'><style id='KdOkP'><dir id='KdOkP'><q id='KdOkP'></q></dir></style></legend>

                  <i id='KdOkP'><tr id='KdOkP'><dt id='KdOkP'><q id='KdOkP'><span id='KdOkP'><b id='KdOkP'><form id='KdOkP'><ins id='KdOkP'></ins><ul id='KdOkP'></ul><sub id='KdOkP'></sub></form><legend id='KdOkP'></legend><bdo id='KdOkP'><pre id='KdOkP'><center id='KdOkP'></center></pre></bdo></b><th id='KdOkP'></th></span></q></dt></tr></i><div id='KdOkP'><tfoot id='KdOkP'></tfoot><dl id='KdOkP'><fieldset id='KdOkP'></fieldset></dl></div>
                      <bdo id='KdOkP'></bdo><ul id='KdOkP'></ul>

                    <small id='KdOkP'></small><noframes id='KdOkP'>

                    <tfoot id='KdOkP'></tfoot>