Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

231
Views
ViewComponent personalizado con asp-for como parámetro

Quiero envolver esto:

 <textarea asp-for="@Model.Content" ...>

en ViewComponent reutilizable, donde la propiedad será el parámetro:

 <vc:editor asp-for="@Model.Content" />

Pude pasar asp-for como parámetro al componente de vista:

 public class EditorViewComponent : ViewComponent { public IViewComponentResult Invoke(ModelExpression aspFor = null) { //when debugging, aspFor has correct value return View(aspFor); } }

Pero no puedo evaluarlo en la vista del componente. Esto no funciona:

 <!-- ViewComponents/Editor/Default.cshtml --> @model Microsoft.AspNetCore.Mvc.ViewFeatures.ModelExpression <textarea asp-for="@Model" />

¿Algunas ideas?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Creo que está mezclando ViewComponents y TagHelpers: https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components

Ver componentes

Los componentes de vista se invocan en los siguientes asuntos:

 @await Component.InvokeAsync("EditorView", @Model.Property); // or <vc:[view-component-name]>

Pruebe el siguiente fragmento:

 <vc:editor for="@Model.Content" />

ayudantes de etiquetas

los asistentes de etiquetas solo se invocan así:

 <textarea asp-for="@Model.Content">
about 4 years ago · Santiago Trujillo Report

0

Si desea pasar ModelExpression a ViewComponent subyacente y luego pasarlo a TagHelper, debe hacerlo usando @TheModelExpression.Model :

 <!-- ViewComponents/Editor/Default.cshtml --> @model Microsoft.AspNetCore.Mvc.ViewFeatures.ModelExpression <textarea asp-for="@Model.Model" />

Como mencionó @JoelHarkes, en este caso particular, el taghelper personalizado podría ser más apropiado. De todos modos, todavía puedo renderizar PartialView ala Template en TagHelper:

 [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()); } }

la plantilla .cshtml se vería exactamente como en viewcomponent.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!