c# - Linq doesn't work in MVC View -
i have list in controller
list<string> mylist = new list<string>(); mylist.add(astring); viewbag.linklist = mylist;
and in view trying do
@viewbag.linklist.first()
and giving me error: 'system.collections.generic.list' not contain definition 'first'
if do
@viewbag.linklist[0]
it works fine.
i put @using system.linq in view. missing something? linq works inside view?
viewbag dynamic. first() extension method on ienumerable<t>. extension methods don't work on dynamic references.
you must first cast list ienumerable<string> before calling first().
@((viewbag.linklist ienumerable<string>).first()) note plain ienumerable not work, since .first() not defined it.
Comments
Post a Comment