MVC 5自定义HtmlHelper,用于轻量级内容编辑

时间:2014-02-11 16:12:46

标签: asp.net-mvc razor html-helper asp.net-mvc-5

在使用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 ... {}语句之间的值将始终呈现。在可以从数据存储中加载内容的情况下,我希望将默认值替换为数据存储中的值。

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

1 个答案:

答案 0 :(得分:2)

您无法按照现在的方式在@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>)