gpt4 book ai didi

c# - ASP.NET 4.5 MVC4 模型绑定(bind)问题 - 无法定义任何模型

转载 作者:太空宇宙 更新时间:2023-11-03 16:26:22 25 4
gpt4 key购买 nike

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

例如:

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

我使用添加 View 创建一个强类型 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 显式转换为该类型,但这似乎违背了使用 @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 派生。添加之后,一切都已修复。感谢您的协助。

最佳答案

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

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

关于c# - ASP.NET 4.5 MVC4 模型绑定(bind)问题 - 无法定义任何模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12450870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com