我正在尝试创建一个自定义 HTML 帮助程序,它将导致 HTML 编辑器出现在 MVC 应用程序中。 我一直在按照以下说明进行操作:http://dan.cx/2012/05/custom-strongly-typed-htmlhelpers-in-asp-net-mvc .我无法让它工作,完全卡住了。
这是我创建的 HtmlHelper.....
public static class HTMLEditor
{
public static HtmlString RenderFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, int? width = 810, int? height = 200)
{
var name = html
.ViewContext
.ViewData
.TemplateInfo
.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
string myEditor = "<textarea id='" + name + "' ";
myEditor += "class='textarea' placeholder='' style='width: " + width + "px !important; height: " + height + "px !important'></textarea>";
myEditor += "<script>$('#" + name + "').wysihtml5({'html':true,'color': false,parser: function(html) {return html;}});";
myEditor += "editor.on('load', function() {editor.focus();editor.composer.commands.exec('insertHTML', '" + metadata.Model + "');})";
myEditor += "</script>";
return new HtmlString(myEditor);
}
}
然后,在我的 Razor View 中,我正尝试像这样使用助手...
@HTMLEditor.RenderFor(model => model.PageDetail.HTML)
然而,一切都编译正常,但是当需要将 View 呈现给浏览器时,我收到一个错误:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1501: No overload for method 'RenderFor' takes 1 arguments
我不确定问题出在哪里。我确实看到 RenderFor 方法有两个参数,但我不确定如何传入模型实例值并在回发后将该值保留在模型实例中。
如有任何帮助,我们将不胜感激。
谢谢 马特
最佳答案
在我看来,您在错误的对象上调用了 .RenderFor。
您的 RenderFor 方法是 HtmlHelper 的扩展方法。因此,您应该从 View 中调用 @Html.RenderFor(model => ...)。
关于c# - 使用模型 Lambda 在 MVC5 中自定义 HTML 帮助程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26660636/