DataBinding: 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'states'.

Last post 03-05-2008 5:07 AM by rjcox. 15 replies.

Sort Posts:

  • DataBinding: 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'states'.

    03-04-2008, 11:41 AM
    • Contributor
      2,727 point Contributor
    • dba123
    • Member since 12-13-2003, 12:04 AM
    • Posts 1,348

    Note: this is a mock-up, thus the hard coded states for testing purposes.

    I'm simply trying to pass up a generic list from my model to my controller and then use that list to bind to a Select.

    Model has:

        public class StatesModel
        {
            public static List<string> RetrieveAllStates()
            {
                List<string> states = new List<string> { "AL", 
                                                         "AK",
                                                         "AZ"};
                return states;
            }
    
        }

    Controller has:

            [ControllerAction]
            public void OrderExample()
            {
                List<string> states = new List<string>();
                states = StatesModel.RetrieveAllStates();
                RenderView("OrderExample", states);
            }

    View has:

    <%=Html.Select("State", ViewData["states"]) %>

    Error: DataBinding: 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'states'.

    When is Microsoft going to get rid of VB.NET!
  • Re: DataBinding: 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'states'.

    03-04-2008, 11:56 AM
    Answer
    • Member
      70 point Member
    • tehlike
    • Member since 11-19-2005, 8:21 AM
    • Posts 25

               List<string> states = new List<string>();
                states = StatesModel.RetrieveAllStates();

                ViewData["states"]=states;
                RenderView("OrderExample");

  • Re: DataBinding: 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'states'.

    03-04-2008, 12:00 PM
    Answer
    • Contributor
      4,382 point Contributor
    • tgmdbm
    • Member since 12-17-2007, 2:08 PM
    • Posts 883
    • ASPInsiders
      TrustedFriends-MVPs

    Yeah. In your code ViewData IS a List<string> you should make you View Strongly Typed ie. ViewPage<List<string>> and in your view use <%= Html.Select("State", ViewData) %>

    OR in your controller use...

    ViewData["states"] = states;
    RenderView("OrderExample");

    ... and the view will stay the same 

  • Re: DataBinding: 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'states'.

    03-04-2008, 12:02 PM
    • Contributor
      4,382 point Contributor
    • tgmdbm
    • Member since 12-17-2007, 2:08 PM
    • Posts 883
    • ASPInsiders
      TrustedFriends-MVPs

     lol, you posted while i was typing.

  • Re: DataBinding: 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'states'.

    03-04-2008, 12:04 PM
    • Contributor
      2,727 point Contributor
    • dba123
    • Member since 12-13-2003, 12:04 AM
    • Posts 1,348

    :)

    Actually I tried to use the Generic ViewPage once like you suggested but it errored out and then I was told this is a bug in the MVC framework that you can't pass a generic string through a generic ViewPage implementation.  Only a Generic list with objects at this point.

     

    For example if I have this:

    ViewPage<List<string>> (in my code behind)

    <%=Html.Select("State", ViewData) %>

    I get a runtime error:

    Parameter count mismatch.

    When is Microsoft going to get rid of VB.NET!
  • Re: DataBinding: 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'states'.

    03-04-2008, 12:07 PM
    • Member
      70 point Member
    • tehlike
    • Member since 11-19-2005, 8:21 AM
    • Posts 25

    i was slightly faster :)

  • Re: DataBinding: 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'states'.

    03-04-2008, 12:24 PM
    • Contributor
      4,382 point Contributor
    • tgmdbm
    • Member since 12-17-2007, 2:08 PM
    • Posts 883
    • ASPInsiders
      TrustedFriends-MVPs

    tehlike:
    i was slightly faster
     

    but less thorough. 

    What I always do, even if just passing a list of strings, is create a view data class. and strongly type against that.

    public class MyViewData{ public List<string> States {get;set;} }

    MyViewData viewData = new MyViewData();
    viewData.States = states;
    RenderView( "OrderExample", viewData );

    so if your View is ViewPage<MyViewData> you can simply use ViewData.States in the view

     

    Typically, when i'm not writting "Example" pages, I'm Always passing in more then just a list of strings, so having the view data class which you can just add properties to is Ideal. 

  • Re: DataBinding: 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'states'.

    03-04-2008, 3:33 PM
    • Contributor
      2,727 point Contributor
    • dba123
    • Member since 12-13-2003, 12:04 AM
    • Posts 1,348

    Thanks for the work around however we're not going to be creating classes just to pass view data.  I get the generic list from the method in my model and that's it.  It's a bit nasty and muddy if you have to start creating classes just to pass in ViewData when the object is meant for the glue between the Controller and View in the first place.

    When is Microsoft going to get rid of VB.NET!
  • Re: DataBinding: 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'states'.

    03-04-2008, 3:40 PM
    • Contributor
      2,727 point Contributor
    • dba123
    • Member since 12-13-2003, 12:04 AM
    • Posts 1,348

    I tried this, and it still doesn't work though.  Maybe I'm just missing something in the syntax but I thought this is what  you recommended:

            [ControllerAction]
            public void OrderExample()
            {
                List<string> states = new List<string>();
                states = StatesModel.RetrieveAllStates();
                ViewData["states"] = states;
                RenderView("OrderExample");
            }
    public partial class OrderExample : ViewPage<List<string>>

    {

    }

    <%=Html.Select("State", ViewData["states"])%>

    Error:

    Compiler Error Message: CS1502: The best overloaded method match for 'System.Collections.Generic.List<string>.this[int]' has some invalid arguments

    Source Error:

    Line 45: 
    Line 46: 			<label for="lastname">State</label>
    Line 47: 			<%=Html.Select("State", ViewData["states"])%>  
    

    When is Microsoft going to get rid of VB.NET!
  • Re: DataBinding: 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'states'.

    03-04-2008, 3:48 PM
    • Contributor
      2,727 point Contributor
    • dba123
    • Member since 12-13-2003, 12:04 AM
    • Posts 1,348

    What a pain in the ass.  So I tried this also.  Not there yet:

            [ControllerAction]
            public void OrderExample()
            {
                List<string> states = new List<string>();
                states = StatesModel.RetrieveAllStates();
                ViewData["states"] = states;
                RenderView("OrderExample");
            }
    public partial class OrderExample : ViewPage<List<string>>

    {

    }

    <%=Html.Select("State", ViewData)%>

    Exception Details: System.ArgumentException: The view data passed into the page is of type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' but this page requires view data of type 'System.Collections.Generic.List`1[System.String]'.
    Parameter name: viewData

    Source Error:

    Line 46:             states = StatesModel.RetrieveAllStates();
    Line 47:             ViewData["states"] = states;
    Line 48:             RenderView("OrderExample");
    Line 49:         }
    

    When is Microsoft going to get rid of VB.NET!
  • Re: DataBinding: 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'states'.

    03-04-2008, 3:53 PM
    • Contributor
      2,727 point Contributor
    • dba123
    • Member since 12-13-2003, 12:04 AM
    • Posts 1,348

    So I still think it's a bug as Fredrick stated in the past, that you can't pass a generic string list to a View in MVC.  Which I would hope MS will fix when the framework goes live.

    tgmdbm, any ideas here or are you also not able to get that to work?  Have you tried it?  I don't think it's possible.

    When is Microsoft going to get rid of VB.NET!
  • Re: DataBinding: 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'states'.

    03-04-2008, 4:58 PM
    • Contributor
      2,727 point Contributor
    • dba123
    • Member since 12-13-2003, 12:04 AM
    • Posts 1,348

    SOB !  Bear with me, I'm just trying to get something to work!

            [ControllerAction]
            public void OrderExample()
            {
                //List<string> states = new List<string>();
                //states = OrderModel.RetrieveAllStates();
                //ViewData["states"] = states;
                String[] states = new String[] { "AL", 
                                                  "AK",
                                                  "AZ"};
                RenderView("OrderExample", states);
            }
    //took out the generic ViewPage since I'm now passing an array of strings

    public partial class OrderExample : ViewPage

    <%=Html.Select("State", ViewData)%>

    Runtime Error: Can't bind to that data source. Consider using an IEnumerable data source such as an Array or collection

    When is Microsoft going to get rid of VB.NET!
  • Re: DataBinding: 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'states'.

    03-04-2008, 6:18 PM
    • Contributor
      4,382 point Contributor
    • tgmdbm
    • Member since 12-17-2007, 2:08 PM
    • Posts 883
    • ASPInsiders
      TrustedFriends-MVPs

    Yeah.

    Clarification, this is NOT a bug with the framework, it's a bug in the Toolkit which has been fixed.

    If you're using the MVCContrib project you could use Html.Form().Select( ... )

    Otherwise, in DataBinder.cs, replace IEnumerable<object> with IEnumerable and your last attempt will work.

  • Re: DataBinding: 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'states'.

    03-04-2008, 7:56 PM
    • Contributor
      2,727 point Contributor
    • dba123
    • Member since 12-13-2003, 12:04 AM
    • Posts 1,348

    thanks.  I guess I should clarify.  The MVC Framework.  Even though it's deemed "Toolkit" it's really a new framework.  I wasn't talking abou the .NET framework...

    I'll give it a try.   but if I change this, will that cause other issues though when passing data to the View, other types?

    When is Microsoft going to get rid of VB.NET!
  • Re: DataBinding: 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'states'.

    03-04-2008, 10:27 PM
    • Contributor
      2,727 point Contributor
    • dba123
    • Member since 12-13-2003, 12:04 AM
    • Posts 1,348
    Finally! Ok then I guess I don't know when to use just ViewData vs. ViewData["listname"] 
           [ControllerAction]
            public void OrderExample()
            {
                String[] states = new String[] { "AL", 
                                                  "AK",
                                                  "AZ"};
                RenderView("OrderExample", states);
            }
    //took out the generic ViewPage since I'm now passing an array of strings

    public partial class OrderExample : ViewPage

    <%=Html.Select("State", ViewData["states"])%>

    When is Microsoft going to get rid of VB.NET!
Page 1 of 2 (16 items) 1 2 Next >