【问题标题】:MVC partial views with custom content具有自定义内容的 MVC 部分视图
【发布时间】:2016-06-15 20:32:18
【问题描述】:

在 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 中做到这一点?

【问题讨论】:

  • 如果您使用的是 MVC 6,您可以制作视图组件。我一想到他们就热。 docs.asp.net/en/latest/mvc/views/view-components.html
  • @JDupont 我真的很高兴。唉,我正在使用 MVC 5(现在)。就我目前所见的 MVC 6 而言,我迫不及待地想要开始使用它!
  • 如果您想要动态内容,可以使用子操作而不是部分视图
  • Html 助手就是你要找的。​​span>
  • @RezaAghaei HtmlHelper 与 MVC

标签: asp.net-mvc webforms


【解决方案1】:

您可以创建HtmlHelper 扩展方法来生成封闭的html,类似于BeginForm() 生成封闭的&lt;form&gt;&lt;/form&gt; 标签的方式。

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 titleButtonType buttons(一个枚举)的参数来在对话框中呈现标题栏和页脚按钮

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    相关资源
    最近更新 更多