0

I have the following code in MVC view. The view is receiving IEnumerable of Anonymous Type. it has 3 fields - TranscriptReqStatusID, TranscriptReqStatusDesc and Count. I am not able to loop and put the data in a Table. It's giving me this error - " Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'TranscriptReqStatusDesc'"

@model IEnumerable<dynamic>
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div>
    <table>
        @foreach (dynamic item in Model)
        {
            <tr>
                <td>@item.TranscriptReqStatusDesc.ToString()</td>
                <td>@item.Count.ToString()</td>                
            </tr>
        }
    </table>
</div>
Yash Dutt
  • 196
  • 1
  • 2
  • 14
  • Don't use `dynamic`. Use a view model with the 3 properties you want. –  Mar 22 '16 at 05:19
  • I don't want to use view model because there will be many such types of views which receive values of returned queries from Controller. Different queries will have different number and types of of fields returned thus I will end up with many view models. – Yash Dutt Mar 22 '16 at 05:22
  • You don't want to do the right thing? Good luck. –  Mar 22 '16 at 05:23
  • View models give you named properties. Without them you're probably going to have to use reflection to interrogate the dynamic and iterate it's contents. – christutty Mar 22 '16 at 05:27
  • Possible duplicate of [How do I iterate over the properties of an anonymous object in C#?](http://stackoverflow.com/questions/2594527/how-do-i-iterate-over-the-properties-of-an-anonymous-object-in-c) – christutty Mar 22 '16 at 05:28
  • @christutty....Hi Chris, I saw the link that you gave above. The code on that site has a foreach loop. The loop is fine but instead of the Console.WriteLine() I want to display the HTML in the razor view as my code above..How can I do that....Please assist... – Yash Dutt Mar 24 '16 at 04:29

0 Answers0