L'ASP.NET Nucleo TagHelper documentation fornisce il seguente esempio:TagHelpers nidificazione in ASP.NET MVC Nucleo 6
public class WebsiteContext
{
public Version Version { get; set; }
public int CopyrightYear { get; set; }
public bool Approved { get; set; }
public int TagsToShow { get; set; }
}
[TargetElement("website-information")]
public class WebsiteInformationTagHelper : TagHelper
{
public WebsiteContext Info { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "section";
output.Content.SetContent(
[email protected]"<ul><li><strong>Version:</strong> {Info.Version}</li>
<li><strong>Copyright Year:</strong> {Info.CopyrightYear}</li>
<li><strong>Approved:</strong> {Info.Approved}</li>
<li><strong>Number of tags to show:</strong> {Info.TagsToShow}</li></ul>");
output.TagMode = TagMode.StartTagAndEndTag;
}
}
Questo può quindi essere utilizzato nel vostro Razor .cshtml come segue:
<website-information info="new WebsiteContext {
Version = new Version(1, 3),
CopyrightYear = 1790,
Approved = true,
TagsToShow = 131 }"/>
Questo genererà il seguente codice HTML:
<section>
<ul>
<li><strong>Version:</strong> 1.3</li>
<li><strong>Copyright Year:</strong> 1790</li>
<li><strong>Approved:</strong> true</li>
<li><strong>Number of tags to show:</strong> 131 </li>
</ul>
</section>
Questa è una sintassi di tag helper piuttosto brutta. C'è un modo per nidificare un altro tag helper e ottenere pieno intelli-sense in modo che l'unico figlio consentito di informazioni sul sito web possa essere contestualizzato? Vedere il seguente esempio:
<website-information>
<context version="1.3" copyright="1790" approved tags-to-show="131"/>
</website-information>
Nel mio caso d'uso, l'elemento sito-informazione ha già molti attributi e voglio aggiungere uno o più elementi annidati separati.
UPDATE
ho sollevato this suggerimento sulla pagina ASP.NET GitHub per implementare questa funzione per TagHelpers.
Perché non aggiungere solo parametri separati all'helper dei tag 'website-information' invece di un singolo parametro' info'? Potresti annidare i tag helper, ma non sarai in grado di forzare che solo gli helper '' siano nidificati nell'helper '' –
@ DanielJ.G. Ci sono molti motivi per farlo. 1. Hai già molti attributi su '' 'website-information'''. 2. Se il contesto ha un senso più logico essendo un elemento figlio 3. Se le proprietà' '' context''' sono logicamente raggruppate insieme 4. Potresti avere più elementi '' 'context'''. –