使用 javascript 调用 ViewComponent

Mar*_*cus 5 javascript asp.net-core

我有一个包含几个视图组件的网页,当我点击这些组件时,我会为它打开一个简单的编辑器,见下图。如果我编辑文本并按回车键,我想重新渲染视图组件而不是孔页面。是否可以使用 javascript 调用视图组件来获得这种行为?

在此处输入图片说明

b.p*_*ell 6

通过更新,您现在应该能够使用 asp.net core 来执行此操作。您可以从 IActionResult 返回一个 ViewComponent,然后只需使用 jQuery 调用它:

[Route("text-editor")]
public IActionResult TextEditor()
{
    return ViewComponent("TextEditorViewComponent");
}
Run Code Online (Sandbox Code Playgroud)

然后可以从您的视图中调用它,就像这样简单:

function showTextEditor() {
    // Find the div to put it in
    var container = $("#text-editor-container");
    $.get("text-editor", function (data) { container.html(data); });
 }
Run Code Online (Sandbox Code Playgroud)

如果您需要将参数传递给 ViewComponent,您可以在 IActionResult 中执行此操作(也许它们会从 View 传递到那里,传递到视图组件等)。


Mar*_*cus 1

这在今天是不可能的,但如果您愿意,您可以为视图组件构建标准代理(同样,您可以为部分视图构建代理)。例如,您对某个操作进行了包罗万象的属性路由。