小编Lau*_*rtz的帖子

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

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

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

@using (Html.EditableSimpleHtml("MyKey"))
{
    <h3>Test</h3>
    <p>
        1<br />
    </p>
}
Run Code Online (Sandbox Code Playgroud)

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

我创建了一个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 …
Run Code Online (Sandbox Code Playgroud)

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

5
推荐指数
1
解决办法
2329
查看次数

标签 统计

asp.net-mvc ×1

asp.net-mvc-5 ×1

html-helper ×1

razor ×1