加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 使用ViewComponent的Ajax

发布时间:2020-12-16 09:42:11 所属栏目:asp.Net 来源:网络整理
导读:如何在Asp.Net Core中使用 AJAX for ViewComponent?那就是调用@ Component.Invoke方法(“ComponentName”,SomeData); ViewComponent将涉及各种视图,具体取决于SomeData而不重新启动主视图. 更新 我的解决方案是: $(function () { $(Container).load('Contr
如何在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 });
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读