How to invoke a View Component from controller

前端 未结 5 1890
耶瑟儿~
耶瑟儿~ 2021-02-13 19:08

Is it possible to invoke a View Component from controller and render it to a string? I am really looking for some code example for this. Any help will be much appreciated.

5条回答
  •  别跟我提以往
    2021-02-13 19:52

    As of beta7 it is now possible to return a ViewComponent directly from a controller. Check the MVC/Razor section of the announcement

    The new ViewComponentResult in MVC makes it easy to return the result of a ViewComponent from an action. This allows you to easily expose the logic of a ViewComponent as a standalone endpoint.

    So now the code for returning the sample view component just needs to be:

    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return ViewComponent("My");
        }
    }
    

提交回复
热议问题