【问题标题】:ASP.NET 4.5 MVC4 Model Binding Issue - Cannot define any modelsASP.NET 4.5 MVC4 模型绑定问题 - 无法定义任何模型
【发布时间】:2012-09-16 21:28:44
【问题描述】:

我遇到了 MVC4 无法绑定到任何东西的问题。

例如:

    public class Question
    {
    [Key]
    [Required]
    public Int32 QuestionID { get; set; }

    [Required]
    public Int32 SurveyID { get; set; }

    [Required]
    public Int32 QuestionNumber { get; set; }

    [Required]  
    public Boolean AssignedToAPage { get; set; }
    }

我使用 Add View 创建了一个强类型视图并选择了上面的类:

    @model Models.Question

    @{
       ViewBag.Title = "View1";
       Layout = "~/Views/Layouts/_Site.Layout.cshtml";
    }

<h2>View1</h2>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>Question</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.SurveyID)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.SurveyID)
        @Html.ValidationMessageFor(model => model.SurveyID)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.QuestionNumber)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.QuestionNumber)
        @Html.ValidationMessageFor(model => model.QuestionNumber)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.AssignedToAPage)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.AssignedToAPage)
        @Html.ValidationMessageFor(model => model.AssignedToAPage)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

因此,在生成此页面后,我尝试访问它并收到以下错误: 编译器错误消息:CS1061:“object”不包含“SurveyID”的定义,并且找不到接受“object”类型的第一个参数的扩展方法“SurveyID”(您是否缺少 using 指令或程序集引用?)

这只是 Visual Studio 2012 RC 的问题吗?我需要重新安装一些东西吗?我已经用谷歌搜索了这个问题的查看时间,但我还没有找到任何相关的东西。我已经诉诸于将模型显式转换为类型,但这似乎违背了拥有@model 的目的。任何输入都会很有用!谢谢。

更新

Apparently I caused the issue by following someone else's advice on creating a custom view page class.  I created the following: 

public abstract class CustomWebViewPage : WebViewPage
{
    public ContentVersionHelper ContentVersion { get; set; }
    public override void InitHelpers()
    {
        base.InitHelpers();
        ContentVersion = new ContentVersionHelper(base.ViewContext, this);
    }
}

public abstract class CustomWebViewPage<TModel> : WebViewPage
    where TModel : class
{
    public ContentVersionHelper<TModel> ContentVersion { get; set; }
    public override void InitHelpers()
    {
        base.InitHelpers();
        ContentVersion = new ContentVersionHelper<TModel>(base.ViewContext, this);
    }
}

甚至没有考虑到我需要从第二类的 WebViewPage 派生。添加后,一切都已修复。感谢您的帮助。

【问题讨论】:

  • 你能显示你的控制器代码吗?

标签: c# asp.net-mvc-4 visual-studio-2012 asp.net-4.5


【解决方案1】:

应更新自定义类以匹配以下内容:

public abstract class CustomWebViewPage : WebViewPage
{
    public ContentVersionHelper ContentVersion { get; set; }
    public override void InitHelpers()
    {
        base.InitHelpers();
        ContentVersion = new ContentVersionHelper(base.ViewContext, this);
    }
}

public abstract class CustomWebViewPage<TModel> : WebViewPage<TModel>
    where TModel : class
{
    public ContentVersionHelper<TModel> ContentVersion { get; set; }
    public override void InitHelpers()
    {
        base.InitHelpers();
        ContentVersion = new ContentVersionHelper<TModel>(base.ViewContext, this);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    相关资源
    最近更新 更多