我想知道是否可以创建一个具有与 Html.BeginForm() 类似的功能和行为的扩展方法,因为它会生成一个完整的 Html 标记,并且我可以在其中指定其内容<% { & } %>
tags.
例如,我可以有这样的视图:
<% using(Html.BeginDiv("divId")) %>
<% { %>
<!-- Form content goes here -->
<% } %>
此功能在我尝试使用示例中生成的功能的上下文中非常有用这个问题
这将使我能够为我将要使用的类型创建容器
<% var myType = new MyType(123, 234); %>
<% var tag = new TagBuilder("div"); %>
<% using(Html.BeginDiv<MyType>(myType, tag) %>
<% { %>
<!-- controls used for the configuration of MyType -->
<!-- represented in the context of a HTML element, e.g.: -->
<div class="MyType" prop1="123" prop2="234">
<!-- add a select here -->
<!-- add a radio control here -->
<!-- whatever, it represents elements in the context of their type -->
</div>
<% } %>
我意识到这会产生无效的 XHTML,但我认为可能还有其他好处,尤其是因为该项目不要求 XHTML 验证 W3C 标准。
Thanks
Dave
不太确定这比简单地定义一个有多少价值<div>
元素,但类似这样
/// <summary>
/// Represents a HTML div in an Mvc View
/// </summary>
public class MvcDiv : IDisposable
{
private bool _disposed;
private readonly ViewContext _viewContext;
private readonly TextWriter _writer;
/// <summary>
/// Initializes a new instance of the <see cref="MvcDiv"/> class.
/// </summary>
/// <param name="viewContext">The view context.</param>
public MvcDiv(ViewContext viewContext) {
if (viewContext == null) {
throw new ArgumentNullException("viewContext");
}
_viewContext = viewContext;
_writer = viewContext.Writer;
}
/// <summary>
/// Performs application-defined tasks associated with
/// freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true /* disposing */);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
/// <param name="disposing"><c>true</c> to release both
/// managed and unmanaged resources; <c>false</c>
/// to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
_disposed = true;
_writer.Write("</div>");
}
}
/// <summary>
/// Ends the div.
/// </summary>
public void EndDiv()
{
Dispose(true);
}
}
/// <summary>
/// HtmlHelper Extension methods for building a div
/// </summary>
public static class DivExtensions
{
/// <summary>
/// Begins the div.
/// </summary>
/// <param name="htmlHelper">The HTML helper.</param>
/// <returns></returns>
public static MvcDiv BeginDiv(this HtmlHelper htmlHelper)
{
// generates <div> ... </div>>
return DivHelper(htmlHelper, null);
}
/// <summary>
/// Begins the div.
/// </summary>
/// <param name="htmlHelper">The HTML helper.</param>
/// <param name="htmlAttributes">The HTML attributes.</param>
/// <returns></returns>
public static MvcDiv BeginDiv(this HtmlHelper htmlHelper, IDictionary<string, object> htmlAttributes)
{
// generates <div> ... </div>>
return DivHelper(htmlHelper, htmlAttributes);
}
/// <summary>
/// Ends the div.
/// </summary>
/// <param name="htmlHelper">The HTML helper.</param>
public static void EndDiv(this HtmlHelper htmlHelper)
{
htmlHelper.ViewContext.Writer.Write("</div>");
}
/// <summary>
/// Helps build a html div element
/// </summary>
/// <param name="htmlHelper">The HTML helper.</param>
/// <param name="htmlAttributes">The HTML attributes.</param>
/// <returns></returns>
private static MvcDiv DivHelper(this HtmlHelper htmlHelper, IDictionary<string, object> htmlAttributes)
{
TagBuilder tagBuilder = new TagBuilder("div");
tagBuilder.MergeAttributes(htmlAttributes);
htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag));
MvcDiv div = new MvcDiv(htmlHelper.ViewContext);
return div;
}
}
并像这样使用
<% using (Html.BeginDiv(new Dictionary<string, object>{{"class","stripey"}}))
{ %>
<p>Content Here</p>
<% } %>
将渲染
<div class="stripey">
<p>Content Here</p>
</div>
或没有 html 属性
<% using (Html.BeginDiv())
{ %>
<p>Content Here</p>
<% } %>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)