M Kenyon II
M Kenyon II

Reputation: 4254

MVC3 ASPX view object does not contain definition

I started this project using MVC2, but changed to the MVC3 dll.

I have an Asset entity. In the controller I have the Details ActionResult defined like this:
EDIT: (Put the correct controller code.)

    public ActionResult Details(int id)
    {
        using (var db = new AssetsContainer())
        {
            return View(db.Assets.Find(id));
        }
    } 

My Details.aspx page is defined this way:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<CITracker.Models.Asset>" %>  
<%@ Import Namespace="CITracker.Models" %>  
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">  
Details  
</asp:Content>  

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Details</h2>

<fieldset>
    <legend>Fields</legend>

    <div class="display-label">Id</div>
    <div class="display-field"><%: Model.Id %></div>

I get this error:

CS1061: 'object' does not contain a definition for 'Id' and no extension method 'Id' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

Any ideas why this was working with MVC2 but fails with MVC3? I don't get develop time errors, but I do get runtime errors.

Upvotes: 3

Views: 1301

Answers (2)

Slider345
Slider345

Reputation: 4748

This is an old question, but I ran into the same problem converting some MVC 2 code to MVC 3. A simple configuration problem to look for is making sure you have this app setting in your config:

<appSettings>
    <add key="enableSimpleMembership" value="false" />
</appSettings>

See this thread for more info.

Upvotes: 0

Anand
Anand

Reputation: 14915

I am not sure, but looks like this is the problem. You are passing a List to the view

return View(db.Assets.ToList()); 

but your Inherits says Inherits="System.Web.Mvc.ViewPage<CITracker.Models.Asset>" instead of

Inherits="System.Web.Mvc.ViewPage<IEnumerable<CITracker.Models.Asset>>"

And also since your model is now a IEnumerable, you can not do a simple Model.Id instead it should be Model[0].Id (or what ever ith elemnt you choose or you do a foreach)

Upvotes: 3

Related Questions