Model binding and nested properties RRS feed

  • Question

  • User313025725 posted

    I'm using model binding in ASP.NET 4.5, and it seems like nested properties are finally supported in data fields. Here is an example:

    <asp:GridView AutoGenerateColumns="False" EnableViewState="True"
        ID="LogLines" ItemType="LogLine" runat="server"
            <asp:BoundField DataField="Time" HeaderText="Time (UTC)">
                <HeaderStyle Width="14em"/>
            <asp:BoundField DataField="User.Username" HeaderText="User">
                <HeaderStyle Width="8em"/>

    User.Username is a nested property and I'm displaying it directly in the GridView. Everything works as expected, except ReSharper flags it as an error and says "Cannot resolve symbol 'User.Username'". When I reported it as a bug they said using nested properties is undocumented behavior and won't be supported in their product. They pointed me to documentation that was pre model binding, so I'm not sure if they are correct or not.

    I'm having a really hard time finding good documentation on model binding in ASP.NET 4.5. Is this really undocumented behavior? Everything works fine, but I have to admit I have had to experiment and search around a lot to figure out how to use model binding because of the poor official documentation. Does anyone know if this is officially supported or not? Or where to find good documentation that I can look through?

    Friday, May 27, 2016 9:38 PM

All replies

  • User36583972 posted

    Hi superhuey,

    Does anyone know if this is officially supported or not? Or where to find good documentation that I can look through?

    As far as I know. You can try the following two methods.

    1: Use DataBinder.Eval in an itemtemplate to access the nested property instead of boundfield.

    <p><%#DataBinder.Eval(Container.DataItem, "NestedClass.Name")%></p>

    2: Create a custom class which inherits BoundField and overrides GetValue to use DataBinder.Eval

    You can refer the following tutorial.

    Fixing BoundField Support for Composite Objects:


    Best Regards,

    Yohann Lu

    Saturday, May 28, 2016 9:36 AM
  • User313025725 posted

    Thanks, but the code I included actually works the way it is. I'm just wondering if it's officially supported behavior or not, or if ReSharper is wrong in flagging it as an error. If it's not supported with model binding I'd rather change it so I'm not relying on undocumented behavior, but if it's supported I'd keep it as is.

    Saturday, May 28, 2016 5:12 PM
  • User36583972 posted

    Hi superhuey,

    I suggest you can visit the Advanced ASP.NET for getting better support.


    Best Regards,

    Yohann Lu

    Monday, May 30, 2016 7:38 AM
  • User739391854 posted

    Have this same problem...

    Tuesday, June 7, 2016 3:57 PM
  • User739391854 posted

    ItemTemplate does work although it makes for some really messy and overblown code compared to a BoundField.

    That 2nd article is 9 years old...

    Tuesday, June 7, 2016 4:50 PM