109221793
109221793

Reputation: 16897

Html.DisplayFor Compile Error - MVC 2

I'm getting the following error:

The type arguments for method 'System.Web.Mvc.Html.DisplayExtensions.DisplayFor<TModel,TValue>(System.Web.Mvc.HtmlHelper<TModel>, System.Linq.Expressions.Expression<System.Func<TModel,TValue>>, string)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Here is my partial View:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyApp.Data.Person>" %>
<fieldset>

    <legend>Information</legend>

    <div class="display-label">ID</div>
    <div class="display-field"><%: Model.ID %></div><br />

    <div class="display-label">Name</div>
    <div class="display-field"><%: Model.Name %></div><br />

    <div class="display-label">DOB</div>
    <div class="display-field"><%: String.Format("{0:g}", Model.DOB) %></div><br />


    <div class="display-label">On Alert</div>
    <div class="display-field"><%: Html.DisplayFor(Model.OnAlert, "FormBoolean") %></div><br />

    <div class="display-label">Deactivated</div>
    <div class="display-field"><%: Model.Deactivated %></div><br />

</fieldset>

Here is my FormBoolean display template:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Boolean>" %>
<div class="formValueShort"><%= Html.Encode(Model ? "Yes" : "No") %></div>

Can anyone shed any light on this? I've also tried 'LabelFor' (without including 'FormBoolean'), HiddenFor, EditorFor - all give the same message. It's strange because I have used these through my project so I'm not sure why it's stuck on this particular one.

Let me know if I need to provide any more information!

Upvotes: 3

Views: 2321

Answers (1)

nemesv
nemesv

Reputation: 139768

The first parameter of DisplayFor is a lambda expression. You need to call like this:

<%: Html.DisplayFor(m => m.OnAlert, "FormBoolean") %>

Upvotes: 8

Related Questions