具有自定义内容的MVC部分视图

时间:2016-06-15 20:32:18

标签: asp.net-mvc webforms

在WebForms中,我可以创建一个可以嵌入自己内容的组件

示例

<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中执行此操作?

1 个答案:

答案 0 :(得分:4)

您可以创建HtmlHelper扩展方法来生成封闭式html,类似于BeginForm()生成封闭式<form></form>代码的方式。

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>");  
        }

    }
}

并在视图中将其用作

@using (Html.BeginDialog())
{
    // add the content to be rendered in the dialog here
}

注意:在web.config文件中添加程序集的命名空间,这样您就不必在视图中包含@using语句。

<namespaces>
    <add namespace="System.Web.Mvc" />
    ....
    <add namespace="YourAssembly.Html" /> <!--add-->
</namespaces>

然后你可以通过创建额外的重载来扩展它,例如你可能还有string title的参数和ButtonType buttons(枚举)来在对话框中呈现标题栏和页脚按钮< / p>

相关问题