加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 用于轻量级内容编辑的MVC 5自定义HtmlHelper

发布时间:2020-12-16 03:23:29 所属栏目:asp.Net 来源:网络整理
导读:经过多年与WebForms的合作,我最近开始向MVC过渡.我正在尝试创建一个可插入的轻量级内容编辑模块,但我遇到了一些问题. 这个想法很简单:创建一个名为EditableSimpleHtml的HtmlHelper,可以在@using … {}语法中使用,以便在剃刀视图中实现以下功能: @using (Ht
经过多年与WebForms的合作,我最近开始向MVC过渡.我正在尝试创建一个可插入的轻量级内容编辑模块,但我遇到了一些问题.

这个想法很简单:创建一个名为EditableSimpleHtml的HtmlHelper,可以在@using … {}语法中使用,以便在剃刀视图中实现以下功能:

@using (Html.EditableSimpleHtml("MyKey"))
{
    <h3>Test</h3>
    <p>
        1<br />
    </p>
}

{…}之间的值是在数据存储中找不到内容时的默认值.

我创建了一个HtmlHelper.以下是简化版本:

public static IDisposable EditableSimpleHtml(this HtmlHelper helper,string key)
{
    // Get the content from the data storage using the key (I will not show the provider itself,its just a provider that will access a db)
    var provider = ContentEditing.Provider;
    string value = provider.GetValue(key);

    if (value == null)
    {
        // No value found in the data storage for the supplied key,we have to use the default value from within the @using... { } statement
        // Can I get that value here? I want to to store it initialy in the data storage

        value = "..."; // How to get html from within the @using... { }?
    }

    return new ContentEditableHtmlString(helper,value);
}

public class ContentEditableHtmlString : IDisposable
{
    private readonly HtmlHelper _helper;

    public ContentEditableHtmlString(HtmlHelper helper,string value)
    {
        _helper = helper;

        var builder = new TagBuilder("div");              
        var writer = _helper.ViewContext.Writer;
        writer.Write(builder.ToString(TagRenderMode.StartTag));
        writer.Write(value);
    }

    public void Dispose()
    {
        _helper.ViewContext.Writer.Write("</div>");
    }
}

问题是我无法从HtmlHelper中的@using … {}语句中获取(默认)内容,或者至少我不知道如何.我需要它,以防我想最初将它存储到数据库.

第二个问题是@using … {}语句之间的值将始终呈现.在可以从数据存储器加载内容的情况下,我希望将默认值替换为来自数据存储器的值.

有没有办法实现这一点,还是我开始走错了路?

解决方法

您无法按照现在的方式在@using {…}语句中获取html.

你可以做的最接近的事情是使用Templated Razor Delegates

public static HelperResult EditableSimpleHtml(this HtmlHelper helper,string key,Func<string,HelperResult> template)
{
    var templateResult = template(null);
    //you have your value here that you can return directly
    //or you can return HelperResult to write to the response directly
    var templateResultHtml = templateResult.ToHtmlString(); 

    return new HelperResult(writer =>
    {
        templateResult.WriteTo(writer);
    });
}

在你看来:

@Html.EditableSimpleHtml("MyKey",@<text>
                                   <h3>Test</h3>
                                   <p>@DateTime.Now</p>
                                   </text>)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读