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

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

在使用 WebForms 多年后,我最近开始向 MVC 过渡。我正在尝试创建一个可插入的轻量级内容编辑模块,但遇到了一些问题。

这个想法很简单:创建一个名为 EditableSimpleHtml 的 HtmlHelper,它可以在 @using... { } 语法中使用,以便在 Razor View 中实现以下目标:

@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>)

关于asp.net-mvc - 用于轻量级内容编辑的 MVC 5 自定义 HtmlHelper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21707084/

相关文章:

asp.net-mvc - ASP.NET MVC - 具有不同输出模型的类型安全 Html.TextBoxFor

asp.net-mvc - 带有 Html.EditorFor() ASP.NET MVC 助手的可空 DateTime

javascript - 保存到 View 中的多个隐藏字段

javascript - 在 Razor 中,接受不带小数的整数

c# - 如何在自定义 HtmlHelper 中使用 C# 对象?

asp.net-mvc-2 - 我如何在 asp.net mvc2 中的 Controller 之外获取请求对象

asp.net-mvc - C# Razor 中 @modeldynamic 的 VB 等价物是什么?

c# - 如何从 DropDownList 中获取所选项目?

asp.net-mvc - 强制 SquishIt 刷新/重新创建缩小的文件

asp.net-mvc-3 - 在隐藏字段中存储列表会导致奇怪的结果