Can't use lambda with EmailBox

40 views
Skip to first unread message

Carl B

unread,
Jun 11, 2012, 5:07:00 PM6/11/12
to mvccontrib-discuss
Form doesn't get much simpler than this:

@model Events.ViewModels.FindMyRegistrationsViewModel

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@this.EmailBox(x => x.Email).AutoLabel()
@Html.ValidationMessageFor(x => x.Email)
}

This form fails to load CS1660: Cannot convert lambda expression to
type 'string' because it is not a delegate type

I can do this:

@model Events.ViewModels.FindMyRegistrationsViewModel

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@this.EmailBox("Email").AutoLabel() @Html.ValidationMessageFor(x =>
x.Email)
}

and it works fine, but I want strong types! (FWIW "Email" is a public
string { get; set; } in the ViewModel)

I obviously have MvcContrib.FluentHtml in my web.config or the latter
wouldn't work. I've gotta be missing something obvious, but searching
for this error seems to indicate a hundred different causes for a
hundred different scenarios, not all of which are MvcContrib related.

Tim Scott

unread,
Jun 11, 2012, 6:41:49 PM6/11/12
to mvccontri...@googlegroups.com
Carl,

This is well covered by tests, and I tried it myself to double check.  No problem.

If you are correct that Email is a public read-write string property of view model T then the only other think of is that your view is not an IViewModelContainer<T>.  It should be our built-in ModelWebViewPage<T> (assuming Razor) or your own implementation.

Tim
--
Contact Jeffrey Palermo or Eric Hexter with specific questions about the MvcContrib project. Or go to http://mvccontrib.org

To unsubscribe from this group, send email to mvccontrib-disc...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mvccontrib-discuss?hl=en

Carl Bussema

unread,
Jun 11, 2012, 7:36:17 PM6/11/12
to mvccontri...@googlegroups.com
So the strongly typed helpers require a view (or a base view) to implement that interface or inherit that? I missed that in the docs, but will try it again tomorrow. I know I tried changing the page to
@Model ModelWebViewPage<FindMyRegistrationsViewModel> and it still didn't work.

Carl Bussema

unread,
Jun 12, 2012, 10:03:04 AM6/12/12
to mvccontri...@googlegroups.com
Ah, for anyone else having this problem you have to use @inherits, not @model. Once I used @inherits ModelWebViewPage<MyViewModel> it worked as intended.

I would like to hear from the MVCcontrib team why they went with that approach and why everything is an extension method on IViewModelContainer instead of HtmlHelper... it's a little confusing switching back and forth between @this and @Html. Since I don't see a significant advantage for example, in using @this.ValidationMessage vs @Html.ValidationMessageFor, I'd rather stick with the official once, since it's well-documented and less prone to random change. It's not a huge deal, but it is confusing to someone used to programming in Razor to have to mentally shift gears half the time. 

Thanks,
Carl

Tim Scott

unread,
Jun 12, 2012, 10:55:01 AM6/12/12
to mvccontri...@googlegroups.com
The simple answer is that FluentHtml predates HtmlHelper<T> by about 1.5 years.  Another answer is that we provide custom behaviors injected into the view that you can do a lot of cool stuff with.
Reply all
Reply to author
Forward
0 new messages