【问题标题】:MVC View: Type arguments Html helper DisplayFor cannot be inferred from the usageMVC 视图:类型参数 Html 帮助器 DisplayFor 无法从用法中推断出来
【发布时间】:2012-03-20 00:48:40
【问题描述】:

我正在尝试在此视图中使用扩展的 HTML Helper DisplayFor:

<%@ 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

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;
        }
    }
}

它给了我这个错误

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

我不知道出了什么问题。请注意,我能够以强类型的方式访问模型,并在视图中弹出 IntelliSense。但是,当我输入 lambda 表达式时,IntelliSense 没有弹出。

【问题讨论】:

  • 我最近发现视图确实可以正常构建并按预期工作,唯一的问题是设置视图时没有 IntelliSense 弹出窗口和讨厌的红线/错误..

标签: model-view-controller c#-4.0 lambda viewmodel html-helper


【解决方案1】:

我现在可以工作了,问题是该项目仍然使用 .NET v3.5 而不是 v4.0 编译,请参阅:

https://stackoverflow.com/a/7142200/1232507

【讨论】:

  • 我也面临同样的问题。您能否以外行的方式指导我如何解决此问题。让步步为营。我真的很感激。
猜你喜欢
  • 2012-10-19
  • 2016-05-09
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多