C# 尝试创建一个HtmlHelper来包装一个公共模式
我试图使用以下代码创建一个名为EditBlockFor的HTMLHelper方法,该方法接受模型并在一次过程中创建LabelFor和EditorFor,而不是每次都必须同时执行这两个操作。当需要从helper.labelf获取HtmlString时,我认为我正在正确地传递HtmlHelp,因为我没有可用的方法标签。我错过了什么C# 尝试创建一个HtmlHelper来包装一个公共模式,c#,asp.net-mvc,C#,Asp.net Mvc,我试图使用以下代码创建一个名为EditBlockFor的HTMLHelper方法,该方法接受模型并在一次过程中创建LabelFor和EditorFor,而不是每次都必须同时执行这两个操作。当需要从helper.labelf获取HtmlString时,我认为我正在正确地传递HtmlHelp,因为我没有可用的方法标签。我错过了什么 using System; using System.Collections.Generic; using System.Linq.Expressions; using
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
namespace MyTest.Code
{
public static class HtmlHelperExtensions
{
public static IHtmlString EditBlockFor<T>
(
this HtmlHelper<T> helper,
Expression<System.Func<T, String>> prop,
Dictionary<String, Object> htmlAttributes = null
)
{
HtmlString label;
HtmlString item;
label = helper.LabelFor(prop, htmlAttributes);
item = helper.EditorFor(prop, htmlAttributes);
return new HtmlString(label+" "+item);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq.Expressions;
使用System.Web;
使用System.Web.Mvc;
命名空间MyTest.Code
{
公共静态类HtmlHelperExtensions
{
公用静态IHtmlString EditBlockFor
(
这个HtmlHelper助手,
表情道具,
字典HtmlatAttributes=null
)
{
HtmlString标签;
HtmlString项目;
label=helper.LabelFor(prop,htmlAttributes);
item=helper.EditorFor(prop,htmlAttributes);
返回新的HtmlString(标签+“”+项);
}
}
}
忘记一个包含
使用System.Web.Mvc.Html