2

我遇到了 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'的定义,并且没有扩展方法'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 派生。添加后,一切都已修复。感谢您的帮助。

4

1 回答 1

0

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

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);
    }
}
于 2012-09-21T21:02:33.930 回答