Reputation: 31
I'm having trouble with MVC4 not being able to bind to anything.
For Example:
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; }
}
I create a strongly typed view using Add View and select the above class:
@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>
So, after generating this page, I try going to it and I get the following error: Compiler Error Message: CS1061: 'object' does not contain a definition for 'SurveyID' and no extension method 'SurveyID' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
Is this just an issue with Visual Studio 2012 RC? Do I need to reinstall something? I've googled this issue a view times but I've yet to find anything relevant. I've resorted to explicitly casting Model to the type but that seems to defeat the purpose of having @model. Any input would be useful! Thanks.
Update
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);
}
}
Didn't even taken into consideration that I needed to derive from WebViewPage on the 2nd class. After adding that, everything was fixed. Thanks for the assistance.
Upvotes: 2
Views: 1215
Reputation: 31
The custom classes should be updated to match 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<TModel>
where TModel : class
{
public ContentVersionHelper<TModel> ContentVersion { get; set; }
public override void InitHelpers()
{
base.InitHelpers();
ContentVersion = new ContentVersionHelper<TModel>(base.ViewContext, this);
}
}
Upvotes: 0