html-helper


How to create an extension method on IHtmlHelper<dynamic>


This article shows how to create an extension method on HtmlHelper<dynamic>, but it doesn't appear to work with MVC6 (I changed HtmlHelper to IHtmlHelper).
The error is:
'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>'
How is this done in MVC6?
The extension method needs to be on IHtmlHelper and not on 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());
}
Usage:
#(Html.CustomSelectList<Tag>("myId", Model, t => t.Name, t => t.Id.ToString()))

Related Links

How can i resize image in html-helper tagbuilder
TextBoxFor title to display the current text
Converting C# Razor view syntax for checkboxlist into VB.Net
Table of content is not proper in chm file
Web helpers for MVC 5?
asp.net mvc3 or mvc4 html helper is not working
Configure the Javascript function + Html Helper
Displaying multiple values with Telerik ASP.Net MVC extensions with the ForeignKey column
Custom html helpers in MVC 4
Return multiple checkbox from Custom html helper in mvc 3r
Getting Class Property Values from Linq Expression In Html Helper MVC3
format htmlhelper date field to dd/mm/yyyyy
How to create internal Linking inside CHM help file
simple_html_dom timer?
Printing Html with razor view engine
How to set the default page for a .CHM file in HTML Help Workshop?

Categories

HOME
ms-access
winforms
jdo
nullpointerexception
hashmap
octobercms
rsync
programming-languages
alpha
azure-media-services
applepay
izpack
imacros
zend-framework3
windows-phone-7
hex-editors
vaadin7
undefined
introduction
php-openssl
wijmo
emgucv
sparse-matrix
selectedindexchanged
chromebook
url-scheme
brunch
linkerd
applozic
saas
kendo-datasource
procdump
javascriptcore
hybridauth
uiswipegesturerecognizer
mapbox-gl
catalog
qwerty
gesture
unobtrusive-validation
opshub
nand2tetris
isbn
convertapi
taffy
dropbox-php
mu
statsd
nativeapplication
carrot
knockout-components
directory-structure
feeds
imanage
jspdf-autotable
orthogonal
messenger
vmware-tools
intrusion-detection
google-web-starter-kit
skype4py
oauth2client
wapiti
spim
metaclass
tmuxinator
quicklisp
riak-cs
createprocessasuser
census
javax.mail
tablelayout
android-imagebutton
mdt
preferences
code-access-security
undefined-reference
hyprlinkr
html5-notifications
page-layout
bulkloader
limejs
distutils
unc
isnullorempty
propertyeditor
code-cleanup
bigcouch
subgurim-maps
joyent
cinema-4d
locationlistener
appender
table-footer
yslow
rescale
privilege
text-coloring
zune
post-build
mdac

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App