I'm writing an ASP.NET MVC 2 site where I have defined a custom ViewModel that I'm trying to use in a View. Unfortunately, I'm running into an error when using the ViewModel through a strongly-typed lambda expression.
My Code
Here's my ViewModel:
[CompareProperties(ComparisonProperty1="EmailAddress", ComparisonProperty2="ConfirmEmailAddress",LessThanAllowed=false,EqualToAllowed=true,GreaterThanAllowed=false,AllowNullValues=true,ErrorMessage="You must enter the same email address in both fields.")]
public class OpenIdRegistrationViewModel
{
[Required(ErrorMessage="You must specify an email address.")]
[DisplayName("Email address")]
[IsValidEmailAddress(ErrorMessage="This is not a valid email address.")]
public string EmailAddress
{
get;
set;
}
[Required(ErrorMessage = "You must enter you email address a second time.")]
[DisplayName("Confirm email address")]
[IsValidEmailAddress(ErrorMessage = "This is not a valid email address.")]
public string ConfirmEmailAddress
{
get;
set;
}
[DisplayName("Nickname")]
[Required(ErrorMessage="You must choose a Nickname.")]
public string Username
{
get;
set;
}
[Required(ErrorMessage="An OpenID claim must be included.")]
public Identifier OpenIdClaim
{
get;
set;
}
}
I'm trying to use the ViewModel in a View like this:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<OpenidRegistrationViewModel>" %>
<asp:Content ID="registerContent" ContentPlaceHolderID="MainContent" runat="server">
<%= Html.ValidationSummary("Account creation was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm()) { %>
<div>
<fieldset>
<legend>Account Information</legend>
<p>
<%= Html.LabelFor(m=>m.Username) %>
<%= Html.TextBoxFor(m=>m.Username)%>
<%= Html.ValidationMessageFor(m=>m.Username) %>
</p>
<!-- etc. -->
<p>
<input type="submit" value="Register" />
</p>
</fieldset>
</div>
<% } %>
</asp:Content>
The Error
However, I'm getting a compiler error on each of the lines where I use a lambda expression (such as Html.LabelFor
, Html.TextBoxFor
, etc.). Here's the error:
The type arguments for method 'System.Web.Mvc.Html.InputExtensions.TextBoxFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
I don't understand what's wrong. Do you see any errors in my code?
Thanks in advance.