asp.net – 使用ViewComponent的Ajax

如何在Asp.Net Core中使用
AJAX for ViewComponent?那就是调用@ Component.Invoke方法(“ComponentName”,SomeData); ViewComponent将涉及各种视图,具体取决于SomeData而不重新启动主视图.

更新

我的解决方案是:

$(function () {
      $(Container).load('ControllerName/ControllerAction', {
                ArgumentName: ArgumentValue });
                                                 });

控制器:

public IActionResult ControllerAction(string value)
        {
           return ViewComponent("ViewComponent", value);
        }

有没有办法在以前的版本中直接使用ViewComponent作为AjaxHelpers?

最佳答案 你的解决方案是对的.
From the docs:

[ViewComponents are] not reachable directly as an HTTP endpoint, they’re invoked from your code (usually in a view). A view component never handles a request.

While view components don’t define endpoints like controllers, you can easily implement a controller action that returns the content of a ViewComponentResult.

正如您所建议的那样,a lightweight controller可以作为ViewComponent的AJAX代理.

public class MyViewComponentController : Controller 
{
    [HttpGet]
    public IActionResult Get(int id) 
    {
        return ViewComponent("MyViewComponent", new { id });
    }
}
点赞