最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net core - Invoke ViewComponent using javascript - Stack Overflow

programmeradmin2浏览0评论

I have a web page with a couple of view ponents, when I click on these ponents I open a simple editor for it, see image below. If I edit the text and hit enter, I would like to rerender the view ponent and not the hole page. Is it possible to invoke a view ponent using javascript to get this behaviour?

I have a web page with a couple of view ponents, when I click on these ponents I open a simple editor for it, see image below. If I edit the text and hit enter, I would like to rerender the view ponent and not the hole page. Is it possible to invoke a view ponent using javascript to get this behaviour?

Share Improve this question asked Feb 17, 2015 at 18:10 marcusmarcus 10.1k9 gold badges59 silver badges110 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

With updates, you should now be able to do this with asp core. You can return a ViewComponent from an IActionResult, then just call it with jQuery:

[Route("text-editor")]
public IActionResult TextEditor()
{
    return ViewComponent("TextEditorViewComponent");
}

Then this can be called from your View with something as simple as this:

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

If you need to pass parameters to the ViewComponent you can do so in the IActionResult (maybe they trickle through from the View to there, to the view ponent, etc).

This is not possible today but you could build a standard proxy for viewponents if you'd like though (similarly you could build a proxy to partial views). e.g. you a catchall attribute route on an action.

发布评论

评论列表(0)

  1. 暂无评论