是否可以从控制器调用View Component并将其渲染为字符串?我真的在寻找一些代码示例。任何帮助将不胜感激。
答案 0 :(得分:7)
从beta7开始,现在可以直接从控制器返回ViewComponent。检查the announcement
的MVC / Razor部分MVC中的新ViewComponentResult可以轻松返回结果 来自动作的ViewComponent。这样可以轻松曝光 ViewComponent的逻辑作为独立端点。
所以现在返回示例视图组件的代码只需要:
public class HomeController : Controller
{
public IActionResult Index()
{
return ViewComponent("My");
}
}
答案 1 :(得分:4)
你可以这样做但你必须应用以下内容,因为它是由DefaultViewComponentHelper渲染的。
您必须创建此实例并创建您需要IViewComponentSelector和IViewComponentInvokerFactory。
要做到这一点,我做了以下事情。
public class HomeController : Controller
{
Microsoft.AspNet.Mvc.DefaultViewComponentHelper helper = null;
Microsoft.AspNet.Mvc.Razor.RazorView razorView = null;
public HomeController(IViewComponentSelector selector,IViewComponentInvokerFactory factory,IRazorPageFactory razorPageFactory,IRazorPageActivator pageActivator,IViewStartProvider viewStartProvider)
{
helper = new DefaultViewComponentHelper(selector, factory);
razorView = new Microsoft.AspNet.Mvc.Razor.RazorView(razorPageFactory, pageActivator, viewStartProvider);
}
public IActionResult Index()
{
ViewContext context = new ViewContext(ActionContext, razorView, ViewData, null);
helper.Contextualize(context);
string st1 = helper.Invoke("My", null).ToString();
return View();
}
}
以下是我的示例View Component。
public class MyViewComponent : ViewComponent
{
public MyViewComponent()
{
}
public IViewComponentResult Invoke()
{
return Content("This is test");
}
}
答案 2 :(得分:3)
请参阅official ASP.NET article on ViewComponent
中的示例在他们的示例中,视图组件直接从控制器调用,如下所示:
public IActionResult IndexVC()
{
return ViewComponent("PriorityList", new { maxPriority = 3, isDone = false });
}
答案 3 :(得分:2)
这是我创建的标记帮助程序,用于通过HTML语法嵌入组件。从这样的TagHelper调用应该与Controller的调用紧密匹配。
ViewComponent Tag Helper
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewComponents;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace TagHelperSamples.Web
{
[HtmlTargetElement("component")]
public class ComponentTagHelper : TagHelper
{
private DefaultViewComponentHelper _componentHelper;
[HtmlAttributeName("name")]
public string Name { get; set; }
[HtmlAttributeName("params")]
public object Params { get; set; }
[ViewContextAttribute] // inform razor to inject
public ViewContext ViewContext { get; set; }
public ComponentTagHelper(IViewComponentHelper componentHelper)
{
_componentHelper = componentHelper as DefaultViewComponentHelper;
}
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
_componentHelper.Contextualize(ViewContext);
output.Content.AppendHtml(
await _componentHelper.InvokeAsync(Name, Params)
);
}
}
}
<强>用法强>
<component name="RecentComments" params="new { take: 5, random: true }"></component>
答案 4 :(得分:1)
来自dotnetstep的代码更新了MVC 6.0.0-beta4(VS2015 RC):
public class HomeController : Controller
{
Microsoft.AspNet.Mvc.ViewComponents.DefaultViewComponentHelper helper = null;
public HomeController(IViewComponentDescriptorCollectionProvider descriptorProvider, IViewComponentSelector selector, IViewComponentInvokerFactory invokerFactory)
{
helper = new DefaultViewComponentHelper(descriptorProvider, selector, invokerFactory);
}
public IActionResult Index()
{
ViewContext context = new ViewContext(ActionContext, null, ViewData, null, null);
helper.Contextualize(context);
string st1 = helper.Invoke("My", null).ToString();
return View();
}
}