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

Equivalent for MvcHtmlString in ASP.NET 5?
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

Categories

HOME
ibm-bluemix
pdf
cloud
yahoo-oauth
elm
yarn
rdf
mvvmcross
pivotal-cloud-foundry
pheatmap
dacpac
kentor-authservices
collectd
circular-dependency
fortumo
nhibernate-envers
emulator
grails3
reverse-proxy
visjs
tokenize
excel-2007
chromium-embedded
php-openssl
spark-jobserver
linkerd
fifo
libuv
libssl
vision
kendo-datasource
gsmcomm
flink-streaming
r-raster
lightswitch-2013
bluestacks
google-cloud-endpoints-v2
wixsharp
neuroscience
c11
ansible-playbook
quadratic-programming
android-mediaprojection
flashair
scrollable
dropbox-php
sqldf
spring-cache
color-profile
cubes
infix-notation
ado.net-entity-data-model
phishing
py2app
etsy
nsviewcontroller
ubuntu-10.04
independentsoft
javax.sound.midi
lemon
metaclass
content-length
wyam
method-parameters
angular-local-storage
unity5.2.3
bridge.net
device-orientation
c++03
pick
dd
nsbutton
applescript-objc
issuu
castle
jubula
yorick
braille
jboss-weld
android-2.2-froyo
centos5
multiprocessor
tws
buster.js
back-stack
dataadapter
interface-orientation
drools-planner
gdata-api
cascalog
nsdatecomponents
data-loss
horizontal-accordion
actionview
pyinotify
datareader
visitor-statistic
subviews
bespin
dbisam
.net-1.0

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App