【问题标题】:Invoke ViewComponents using javascript使用 javascript 调用 ViewComponents
【发布时间】:2020-08-19 12:19:35
【问题描述】:

我正在尝试使用 javascript 调用 ViewComponent 以促进页面加载,现在我使用 razor 调用了 viewcomponent 但页面加载需要很长时间,该怎么做?

这是控制器中的方法:

public async Task<IViewComponentResult> InvokeAsync(string id)
{
    //var notifications = _context.Notifications.Include(m => m.ApplicationUser).Include(m => m.Sender).OrderByDescending(m => m.Id);

    return View(await _context.Follows.Include(f=>f.User.University).Include(f=>f.User.Faculty).Where(n=>n.FollowedUserId==id).ToListAsync());
}

和方法调用(jQuery):

<script>
    $(document).ready(function () {
        $('#viewallfollowers').click(
            function () {

                $("#followersrefresh").load("@await Component.InvokeAsync("Follower", new { id=Model.ApplicationUser.Id })");

              });
    });
</script>

【问题讨论】:

    标签: jquery ajax asp.net-core razor asp.net-core-viewcomponent


    【解决方案1】:

    View 组件本质上是一个 razor 页面,一个 razor 页面在运行时呈现 html。因此,您不能使用 js 调用它。但是,您可以重定向到控制器中的 get 操作并返回视图组件。检查类似的SO:

    Invoke ViewComponent using javascript

    【讨论】:

      【解决方案2】:

      ViewComponent不能直接在js中的InvokeAsync中加载,它会在你的页面加载时获取对应的html,而不是在点击事件触发的时候,所以你的页面会加载很长时间,那是你的一个错误js.

      为了实现点击事件加载viewcomponent视图,需要在点击事件中使用ajax get request向控制器申请viewcomponent的return the html代码。

      控制器:

       public IActionResult GetMyViewComponent(string uid)
              {
                  return ViewComponent("Follower", new { id = uid});//it will call Follower.cs InvokeAsync, and pass id to it.
              }
      

      查看:

      @model WebApplication_core_mvc.Models.MyCompModel
      @{
          ViewData["Title"] = "Index";
          Layout = "~/Views/Shared/_Layout.cshtml";
      }
      
      <h1>Index</h1>
      
      @section Scripts{
          <script>
         $(document).ready(function () {
              $('#viewallfollowers').click(function () {
                  {
                      $("#followersrefresh").empty();
                      var _url = '@Url.Action("GetMyViewComponent", "Home")';
                      $.ajax({
                          type: "GET",
                          url: _url,
                          data: { uid: $(this).prop("name") },
                          success: function (result) {
                              $("#followersrefresh").html(result);
                          },
                      });
                  }
              });
          });
          </script>
      }
      <input id="viewallfollowers" type="button" value="button" name="@Model.ApplicationUser.Id"/>
      <div id="followersrefresh" >
      </div>
      

      ViewComponents.Follower.cs:

       public class Follower : ViewComponent
          {
              private readonly MyDbContext _context;
              public Follower(MyDbContext context)
              {
                  _context = context;
              }
              public async Task<IViewComponentResult> InvokeAsync(string id)
              {
                  return View(await _context.Follows.Include(f=>f.User.University).Include(f=>f.User.Faculty).Where(n=>n.FollowedUserId==id).ToListAsync());
              }
          }
      

      这是测试结果:

      【讨论】:

      • 非常感谢您的努力,这是一个完美的答案,我只想告诉您,我已经用我自己的代码更改了 ajax 代码,我不知道为什么您的 ajax 代码没有调用“GetMyViewComponentAsync”方法成功,另一件事,控制器中的方法名称是“GetMyViewComponentAsync”,而在ajax代码中方法名称是“GetMyViewComponent”,因此控制器和ajax中的名称不同,
      • 这是我的 (jQuery , ajax) 代码,如果您不介意,只需将其替换为答案中的 ajax 部分即可,因为我想将您的答案标记为最佳答案
      • $(document).ready(function () { $('#viewallfollowers').click(function () { { $("#followersrefresh").empty(); var _url = ' @Url.Action("GetMyViewComponent", "Home")'; $.ajax({ type: "GET", url: _url, data: { uid: $(this).prop("name") }, 成功:函数(结果){ $("#followersrefresh").html(result); }, }); } }); });
      • @mohammedalani1991,我将GetMyViewComponent 设置为异步操作,当然这里没有效果,async can be deleted,我已经更新了我的答案与你的jquery代码一致,并且测试结果是可行的,可以重新参考。
      • 这肯定会起作用,但它也不必要地使事情复杂化。为什么不直接从控制器返回部分视图?
      猜你喜欢
      • 1970-01-01
      • 2015-02-06
      • 2022-12-21
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      • 2016-12-21
      • 1970-01-01
      相关资源
      最近更新 更多