MVC视图:类型参数Html帮助器DisplayFor不能从用法推断

MVC视图:类型参数Html帮助器DisplayFor不能从用法推断

问题描述:

我想在此视图中使用扩展的HTML助手DisplayFor:

I'm trying to make use of the extended HTML Helper DisplayFor in this View:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcCms.Web.ViewModels.SubscriptionsViewModel>" %>

<% using (Html.BeginForm("TrainingSubscription", "Account", FormMethod.Post))
    { %>
 <%: Html.DisplayFor(m => m.Subscriptions) %>
 <input type="submit" value="Save" />
 <% } %>

使用以下ViewModel

with the following ViewModel

namespace MvcCms.Web.ViewModels
{
    public class SubscriptionsViewModel
    {
        public string TrainingId { get; set; }
        public string Subject { get; set; }      
        public IEnumerable<SubscriptionViewModel> Subscriptions { get; set; }

        public SubscriptionsViewModel(string TrainingId, string Subject, IEnumerable<SubscriptionViewModel> Subscriptions)
        {
            this.TrainingId = TrainingId;
            this.Subject = Subject;
            this.Subscriptions = Subscriptions;
        }
    }

    public class SubscriptionViewModel
    {
        public string ContactId { get; set; }
        public string FullName { get; set; }
        public bool Subscribed { get; set; }

        public SubscriptionViewModel(string ContactId, string FullName, bool Subscribed)
        {
            this.ContactId = ContactId;
            this.FullName = FullName;
            this.Subscribed = Subscribed;
        }
    }
}

/ p>

It's giving me this error


方法的类型参数
'System.Web.Mvc.Html.DisplayExtensions.DisplayFor(System.Web.Mvc.HtmlHelper ,System.Linq.Expressions.Expression>)'
不能根据用法推断。请尝试明确指定类型参数

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

我无法确定错误。注意,我能够以强类型方式访问模型,IntelliSense在视图中弹出。但是,当我输入lambda表达式时,IntelliSense不会弹出。

I can't figure what's wrong. Note that I'm able to access the Model in a strongly typed manner with IntelliSense popping up in the view. However, IntelliSense is not popping up when I'm typing the lambda-expression.

是项目仍然使用.NET v3.5而不是v4.0编译,请参阅:

I got it working now, the problem was that the project still compiled with .NET v3.5 instead of v4.0, see:

http://*.com/a/7142200/1232507