Html.BeginForm () type d'extension
-
22-07-2019 - |
Question
Est-ce que quelqu'un connaît la syntaxe permettant de créer une méthode HtmlHelperextension personnalisée qui se comporte comme ..
<% using (Html.BeginForm()) {%>
<p>Loads of html stuff here </p>
<% } %>
Je pense à quelque chose dans le sens de ....
Des idées?
A bientôt,
ETFairfax
La solution
Vous devez créer une classe qui implémente l'interface IDisposable
et la renvoyer à partir de votre HtmlHelper
.
public static class HtmlHelperTableExtensions {
private class TableRenderer : IDisposable {
HtmlHelper html;
public TableRenderer(HtmlHelper html) {
this.html = html;
}
public void Dispose() {
HtmlHelperTableExtensions.EndTable(html);
}
}
public static IDisposable BeginTable(this HtmlHelper html) {
// print begin table here...
return new TableRenderer(html);
}
public static void EndTable(this HtmlHelper html) {
// print end table here...
}
}
Autres conseils
Il vous faudrait une méthode semblable à celle-ci:
public static IDisposable BeginTable(this HtmlHelper html, ...)
{
// write the start of the table here
return new EndTableWriter();
}
Où EndTableWriter
ressemble à ceci:
private class EndTableWriter : IDisposable
{
public void Dispose()
{
// write the end of the table here
}
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow