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 });
    }
}

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....