具有自定义内容的MVC部分视图 [英] MVC partial views with custom content
问题描述
在WebForms中,我可以创建一个组件,在其中嵌入我自己的内容
In WebForms, I could create a component where I could embed my own content
示例
<uc:BootstrapModal Title="Hello World" Size="Large">
<h1>Hello World</h1>
</uc:BootstrapModal>
<!--generates this...-->
<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<h1>Hello World</h1>
</div>
</div>
</div>
如何在MVC中做到这一点?
How can I do this in MVC?
推荐答案
您可以创建HtmlHelper
扩展方法来生成封闭的html,类似于BeginForm()
生成封闭的<form></form>
标记的方法.
You can create a HtmlHelper
extension method to generate the enclosing html, similar to the way BeginForm()
generates enclosing <form></form>
tags.
using System;
using System.Web.Mvc;
namespace YourAssembly.Html
{
public class Dialog : IDisposable
{
private readonly ViewContext _viewContext;
private bool _disposed;
public Dialog(ViewContext viewContext)
{
if (viewContext == null)
{
throw new ArgumentNullException("viewContext");
}
_viewContext = viewContext;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
_disposed = true;
DialogExtensions.EndDialog(_viewContext);
}
}
public void EndDialog()
{
Dispose(true);
}
}
public static class DialogExtensions
{
public static Dialog BeginDialog(this HtmlHelper htmlHelper)
{
return DialogHelper(htmlHelper);
}
private static Dialog DialogHelper(this HtmlHelper htmlHelper)
{
TagBuilder div = new TagBuilder("div");
div.AddCssClass("modal fade bs-example-modal-lg");
div.MergeAttribute("tabindex", "-1");
div.MergeAttribute("role", "dialog");
htmlHelper.ViewContext.Writer.Write(div.ToString(TagRenderMode.StartTag));
div = new TagBuilder("div");
div.AddCssClass("modal-dialog modal-lg");
htmlHelper.ViewContext.Writer.Write(div.ToString(TagRenderMode.StartTag));
div = new TagBuilder("div");
div.AddCssClass("modal-content");
htmlHelper.ViewContext.Writer.Write(div.ToString(TagRenderMode.StartTag));
Dialog modal = new Dialog(htmlHelper.ViewContext);
return modal;
}
public static void EndDialog(this HtmlHelper htmlHelper)
{
EndDialog(htmlHelper.ViewContext);
}
internal static void EndDialog(ViewContext viewContext)
{
viewContext.Writer.Write("</div>");
viewContext.Writer.Write("</div>");
viewContext.Writer.Write("</div>");
}
}
}
,并且在视图中将其用作
and in the view use it as
@using (Html.BeginDialog())
{
// add the content to be rendered in the dialog here
}
注意:在web.config
文件中,添加程序集的名称空间,这样您就不必在视图中包括@using
语句.
Note: In the web.config
file add the namespace of your assembly so that you do not have to include @using
statements in the view.
<namespaces>
<add namespace="System.Web.Mvc" />
....
<add namespace="YourAssembly.Html" /> <!--add-->
</namespaces>
然后您可以通过创建其他重载来扩展它,例如,您可能还具有string title
和ButtonType buttons
(一个枚举)的参数,以在对话框中呈现标题栏和页脚按钮
And you can then extend this by creating additional overloads, for example you might also have parameters for string title
and a ButtonType buttons
(an enum) to render a title bar and footer buttons in the dialog
这篇关于具有自定义内容的MVC部分视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!