微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐