MVC视图:类型参数Html帮助器DisplayFor不能从用法推断 [英] MVC View: Type arguments Html helper DisplayFor cannot be inferred from the usage

查看:228
本文介绍了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://stackoverflow.com/a/7142200/1232507

这篇关于MVC视图:类型参数Html帮助器DisplayFor不能从用法推断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆