gle*_*-84 3 html-helper asp.net-core-mvc asp.net-core
本文介绍如何创建扩展方法HtmlHelper<dynamic>,但它似乎不适用于MVC6(我将HtmlHelper更改为IHtmlHelper).
错误是:
'IHtmlHelper<PagedList<Tag>>' does not contain a definition for 'CustomSelectList' and the best extension method overload 'HtmlHelperExtensions.CustomSelectList<Tag>(IHtmlHelper<dynamic>, string, IEnumerable<Tag>, Func<Tag, string>, Func<Tag, string>)' requires a receiver of type 'IHtmlHelper<dynamic>'
Run Code Online (Sandbox Code Playgroud)
这是如何在MVC6中完成的?
gle*_*-84 11
扩展方法需要打开IHtmlHelper而不打开HtmlHelper<dynamic>.
public static HtmlString CustomSelectList<T>(
this IHtmlHelper html,
string selectId,
IEnumerable<T> list,
Func<T, string> getName,
Func<T, string> getValue)
{
StringBuilder builder = new StringBuilder();
builder.AppendFormat("<select id=\"{0}\">", selectId);
foreach (T item in list)
{
builder.AppendFormat("<option value=\"{0}\">{1}</option>",
getValue(item),
getName(item));
}
builder.Append("</select>");
return new HtmlString(builder.ToString());
}
Run Code Online (Sandbox Code Playgroud)
用法:
@(Html.CustomSelectList<Tag>("myId", Model, t => t.Name, t => t.Id.ToString()))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2851 次 |
| 最近记录: |