0

I'm working on ASP.NET MVC project with Entity Framework, the problem is I want to return data from Entity to a viewbag but this data structured by anonymous method (means I create manually the properties and their values that will returned to the viewbag), and the Viewbag will returned to a view, and I want to use these values saved in the Viewbag.

That what I try,

Controller :

public ActionResult SHOW()
{

    var x = XController.xx();

    ViewBag.DATA = x;

    return View();
}

xx function :

public static dynamic xx()
{
    var R = (from U in x_DB_Context.Con.UFs from P in x_Context.Con.FRMTRs select new { CODE = U.CD_UF, INTTL = U.NM_UF, FL = U.FL_UF, ANN = U.ANN_UF, STT = U.STT_UF, SMSTR = U.SMSTR_UF, COEF = U.COEF_UF, NBCTR = U.NB_CTR_UF, FRMTR = P.NM_FRMTR + " " + P.PRN_FRMTR }).ToList();

    return R;
}

View :

@foreach (var item in ViewBag.DATA)
{
    <tr>

        <td>@item.CODE</td>
        <td>@item.INTTL</td>
        <td>@item.FL</td>
        <td>@item.ANN</td>
        <td>@item.SMSTR</td>
        <td>@item.COEF</td>
        <td>@item.NB_CTR</td>
        <td>@item.FRMTR</td>
        <td>@item.STT</td>

    </tr>
}

When I try to access to this view I saw error page with a big header message contains ('object' does not contain a definition for 'CODE')

enter image description here

Please any help, and a lot of thanks.

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
ADS MB
  • 175
  • 11
  • You really need to start by adding some breakpoints and debugging - for instance, what is the value of R just before the action resolves? – Tieson T. Jun 04 '20 at 03:02

1 Answers1

0

you should create classes instead of using anonymous objects and use strong-type models.

In Model,

public class UP
{
    public int CODE{ get; set; }
    public string INTTL{ get; set; }
    public string ANN{ get; set; }
    public string SMSTR{ get; set; }
    public decimal COEF{ get; set; }
    public string NB_CTR{ get; set; }
    public string FRMTR{ get; set; }
    public string STT{ get; set; }

}

In Controller,

public static dynamic xx()
{
    var R = (from U in x_DB_Context.Con.UFs from P in x_Context.Con.FRMTRs select new UP{ CODE = U.CD_UF, INTTL = U.NM_UF, FL = U.FL_UF, ANN = U.ANN_UF, STT = U.STT_UF, SMSTR = U.SMSTR_UF, COEF = U.COEF_UF, NBCTR = U.NB_CTR_UF, FRMTR = P.NM_FRMTR + " " + P.PRN_FRMTR }).ToList();

    return R;
}

In View,

@foreach (var item in (IEnumerable<UP>)ViewBag.DATA)

                            {
                                <tr>

                                    <td>@item.CODE</td>
                                    <td>@item.INTTL</td>
                                    <td>@item.FL</td>
                                    <td>@item.ANN</td>
                                    <td>@item.SMSTR</td>
                                    <td>@item.COEF</td>
                                    <td>@item.NB_CTR</td>
                                    <td>@item.FRMTR</td>
                                    <td>@item.STT</td>

                                </tr>
                            }

If you insist about using dynamic you can take a look at these questions:

Dynamic Anonymous type in Razor causes RuntimeBinderException

Simplest Way To Do Dynamic View Models in ASP.NET MVC 3

Michael Wang
  • 3,782
  • 1
  • 5
  • 15