Custom ViewComponent with asp-for as parameter
我想把它包起来:
1 | <textarea asp-for="@Model.Content" ...> |
到可重用的ViewComponent中,其中property是参数:
1 | <vc:editor asp-for="@Model.Content" /> |
我能够将asp-for作为参数传递给viewcomponent:
1 2 3 4 5 6 7 8 | public class EditorViewComponent : ViewComponent { public IViewComponentResult Invoke(ModelExpression aspFor = null) { //when debugging, aspFor has correct value return View(aspFor); } } |
但是我无法从组件的角度对其进行评估。 这不起作用:
1 2 3 | <!-- ViewComponents/Editor/Default.cshtml --> @model Microsoft.AspNetCore.Mvc.ViewFeatures.ModelExpression <textarea asp-for="@Model" /> |
有任何想法吗?
如果要将ModelExpression传递给基础ViewComponent,然后将其传递给TagHelper,则必须使用
1 2 3 | <!-- ViewComponents/Editor/Default.cshtml --> @model Microsoft.AspNetCore.Mvc.ViewFeatures.ModelExpression <textarea asp-for="@Model.Model" /> |
正如@JoelHarkes提到的那样,在这种特殊情况下,自定义taghelper可能更合适。 无论如何,我仍然可以在TagHelper中渲染PartialView ala模板:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | [HtmlTargetElement("editor", Attributes ="asp-for", TagStructure = TagStructure.WithoutEndTag)] public class EditorTagHelper : TagHelper { private HtmlHelper _htmlHelper; private HtmlEncoder _htmlEncoder; public EditorTagHelper(IHtmlHelper htmlHelper, HtmlEncoder htmlEncoder) { _htmlHelper = htmlHelper as HtmlHelper; _htmlEncoder = htmlEncoder; } [HtmlAttributeName("asp-for")] public ModelExpression For { get; set; } [ViewContext] public ViewContext ViewContext { set => _htmlHelper.Contextualize(value); } public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { output.TagName = null; var partialView = await _htmlHelper.PartialAsync("TagHelpers/Editor", For); var writer = new StringWriter(); partialView.WriteTo(writer, _htmlEncoder); output.Content.SetHtmlContent(writer.ToString()); } } |
然后,.cshtml模板将在viewcomponent中看起来完全一样。
我认为您正在混合使用ViewComponents和TagHelpers:https://docs.microsoft.com/zh-cn/aspnet/core/mvc/views/view-components
查看组件
视图组件在以下情况下被调用:
1 2 3 | @await Component.InvokeAsync("EditorView", @Model.Property); // or <vc:[view-component-name]> |
尝试以下代码段:
1 | <vc:editor for="@Model.Content" /> |
标记助手
标签助手仅按以下方式调用:
1 | <textarea asp-for="@Model.Content"> |