ViewData appears as a class..

Last post 01-10-2008 6:33 PM by edmccaffrey. 8 replies.

Sort Posts:

  • ViewData appears as a class..

    01-10-2008, 1:27 AM
    • Member
      2 point Member
    • edmccaffrey
    • Member since 12-28-2007, 7:51 PM
    • Posts 9

    ViewData should be part of ViewPage<T> (ViewPage.ViewData), unless I am mistaken.  However, I cannot use ViewData.(my data, passed from the controller), because ViewData is appearing as its own class.

     

    Am I missing something? 

  • Re: ViewData appears as a class..

    01-10-2008, 2:58 AM
    • Contributor
      7,054 point Contributor
    • rjcox
    • Member since 12-19-2007, 2:14 PM
    • Basingstoke, UK
    • Posts 1,444

    Have you got a type called "ViewData" in your project?

    If you have, to resolve the ambiguity, using this.ViewData to force access to the property.

    Richard
  • Re: ViewData appears as a class..

    01-10-2008, 6:10 AM
    • Member
      2 point Member
    • edmccaffrey
    • Member since 12-28-2007, 7:51 PM
    • Posts 9

    rjcox:

    Have you got a type called "ViewData" in your project?

    If you have, to resolve the ambiguity, using this.ViewData to force access to the property.




     No, I have no naming conflicts; it is appearing as "class System.Web.Mvc.ViewData," and not any part of my code.

     Using this.ViewData from within the View's .aspx or its code behind yields the same result.
     

  • Re: ViewData appears as a class..

    01-10-2008, 6:38 AM
    • Contributor
      7,054 point Contributor
    • rjcox
    • Member since 12-19-2007, 2:14 PM
    • Basingstoke, UK
    • Posts 1,444

    Do you have a code sample?

    Both  ViewPage and Controller have ViewData properties that should take precedence over the type (which I had not noticed for this reason).

    (Since I'm generally using ViewPage<>... I'm not actually using Controller.ViewData anyway.) 

    Richard
  • Re: ViewData appears as a class..

    01-10-2008, 8:35 AM
    • Member
      2 point Member
    • edmccaffrey
    • Member since 12-28-2007, 7:51 PM
    • Posts 9

    rjcox:

    Do you have a code sample?

    Both  ViewPage and Controller have ViewData properties that should take precedence over the type (which I had not noticed for this reason).

    (Since I'm generally using ViewPage<>... I'm not actually using Controller.ViewData anyway.) 

     

     

    I'll post some sample code later tonight after when I have access to that machine again.  Do you mean that using ViewPage<T> changes the way to access the ViewData?  I'll create some pseudo-code, which will represent what I am doing (typing without editor, please excuse the rough snippets).

     

    Model 

    public List GetFoo(id)
    {
    return Foos.Where(f => f.ID == id).ToList();
    }
     
    Controller
    public class FooController : Controllers
    {
    [ControllerAction]
    public void Index(int id)
    {
    genericDataContext dc = new GenericDataContext();

    var fooList = GetFoos(id);

    RenderView("Index", fooList);
    }
    }
     
    View
    public partial class Index : ViewPage< List<Foo> >
    {
    }
      
    Now, if I try to do something like:
    foreach (Foo f in ViewData)
    {
    string s = f.Bar;
    }
     (tried in both code-behind and inline, with proper modifications)
    Then ViewData appears as a type, and I cannot access it.  My code seems to match what has been posted in ScottGu's tutorials.  Of course, unless I am making a large oversight, and this does work, then there must be something subtle in my code that is off, and I'll have to post it later.
     
    Thanks. 
  • Re: ViewData appears as a class..

    01-10-2008, 8:57 AM
    • Contributor
      7,054 point Contributor
    • rjcox
    • Member since 12-19-2007, 2:14 PM
    • Basingstoke, UK
    • Posts 1,444

    edmccaffrey:
    Do you mean that using ViewPage<T> changes the way to access the ViewData?
     

    No... just the type of the property (Dictionary in ViewPage, but TViewData in ViewPage<TViewData>).

    edmccaffrey:
    foreach (Foo f in ViewData)
    {
    string s = f.Bar;
    }
     

    Which is pretty much what I have 

    namespace MvcApplication.Views.Entry {
    public partial class Index : ViewPage<EntryIndexViewData> {
    public void Page_Load() {
    EntryList.DataSource = ViewData.Entries;
    EntryList.DataBind();
    }
    }
    }
     

    And this works without problem (and I am using System.Web.Mvc).

    What happens if you use "this.ViewData" (or base.ViewData) in place of just ViewData?

    Richard
  • Re: ViewData appears as a class..

    01-10-2008, 9:40 AM
    • Member
      2 point Member
    • edmccaffrey
    • Member since 12-28-2007, 7:51 PM
    • Posts 9

    rjcox:

    And this works without problem (and I am using System.Web.Mvc).

    What happens if you use "this.ViewData" (or base.ViewData) in place of just ViewData?

     

     It will still appear as a type, "class System.Web.Mvc.ViewData"

     
    The code you posted is basically what I have tried.  I can try to bind it in the code behind, or I can try to access it inline, and I get the same problem.  I even copied the first MVC tutorial that ScottGu posted and I had the same problem with that.  Unless it is a very subtle error, this seems like it has to be some sort of configuration problem. 

  • Re: ViewData appears as a class..

    01-10-2008, 10:26 AM
    • Contributor
      7,054 point Contributor
    • rjcox
    • Member since 12-19-2007, 2:14 PM
    • Basingstoke, UK
    • Posts 1,444

    edmccaffrey:
    this seems like it has to be some sort of configuration problem
     

    Make that "bizare configuration problem" and I can't disagree.

    Can you test for this kind of type/member ambiguity in (say) a Console project to enter something like this: 

    namespace ConsoleApplication1 {
    class Foo {
    public int Bar { get; set; }
    }

    class Xyzzy {
    public string Foo { get; set; }
    }
    class Program : Xyzzy {
    private void Run() {
    Foo = "One";
    Foo x = new Foo();
    x.Bar = 1;
    }

    static void Main(string[] args) {
    (new Program()).Run();
    }
    }
    }
     

    (this is the complete code, no using statements needed) which works fine on the system I have MVC installed on.

    Note the use of inherited property Foo and type Foo in Program.Run.

    Richard
  • Re: ViewData appears as a class..

    01-10-2008, 6:33 PM
    • Member
      2 point Member
    • edmccaffrey
    • Member since 12-28-2007, 7:51 PM
    • Posts 9

    As expected, I can build and execute that program just fine.

    Even if I create a completely new app and immediately attempt to access ViewData from within the View, I have this problem.  Along with the rest of this thread, it leads me to just about exclude a programming error.
     

Page 1 of 1 (9 items)