83475

Query Display Error

Question:

I'm getting an error while mounting and displaying this query.

var itens = _context.ContasReceber .Include(x => x.Pessoas) .Include(x => x.PlanosServicos) .Select(c => new { Identificador = c.Pessoas.NIdentificador, NomePessoa = c.Pessoas.Nome, c.Observacao, c.Vencimento, c.Valor, c.Quitado, c.DataPagamento, c.ValorPago }) .ToList(); ViewData["Contas"] = itens;

cshtml:

@foreach (var item in ViewBag.Contas) { <tr> <th> @item.Identificador </th> <th> @item.NomePessoa </th> <th> @item.Observacao </th> <th> @item.Vencimento </th> <th> @item.Quitado </th> <th> @item.DataPagamento </th> <th> @item.ValorPago </th> </tr> }

The error is:

An unhandled exception occurred while processing the request. RuntimeBinderException: 'object' does not contain a definition for 'Identificador' CallSite.Target(Closure , CallSite , object )

Answer1:

In razor pages PageModel, you could use ViewModel to return custom data. Here is a simplified version of the demo.

In cshtml.cs:

public class ContasViewModel { public string Identificador { get; set; } public string NomePessoa { get; set; } public string Observacao { get; set; } } public IList<ContasViewModel> Contas { get; set; } public async Task OnGetAsync() { Contas = await _context.ContasReceber .Include(x => x.Pessoas) .Include(x => x.PlanosServicos) .Select(c => new ContasViewModel { Identificador = c.Pessoas.NIdentificador, NomePessoa = c.Pessoas.Nome, Observacao= c.Observacao }).ToListAsync(); }

In cshtml:

@foreach (var item in Model.Contas) { <tr> <th> @item.Identificador </th> <th> @item.NomePessoa </th> <th> @item.Observacao </th> </tr> }

Answer2:

When iterating over your items foreach (var item in ViewBag.Contas), the type of item is Object. Object has no properties by default. Since you are using an anonymous type, you can change your foreach to foreach (dynamic item in ViewBag.Contas) which will allow you to reference its properties as it will treat item as a dynamic object similar to ViewBag.

Recommend

  • JDBC- cant retrive value and delete table in mysql database
  • Can't navigate using Prism
  • Error in C++/CLI, Can't take address of function unless creating delegate instance using Pthrea
  • angular Js ui router nested views
  • Multiple Views with nested views Angular
  • Disable diagonal dragging in UIScrollview
  • RedirectToAction after validation errors
  • ASP.NET MVC3 HtmlPasswordFor how to make it not reset after post methods
  • Pass Viewdata as member from other viewdata to RenderPartial makes the first null
  • Ember CollectionView with views that have different templates
  • Entity Framework Linq equals value or is null
  • How to find the first character in a given string, that appears only once [closed]
  • Entity Framework and the raw string query - SQL injection prevention
  • How to Convert IList to IList where SomeObject implements ISomeInterface using covariance in C# 4.0
  • Why doesn't this LINQ query work as expected?
  • Use Hidden field in syncfusion column chart series
  • Get Distinct Parent Items using Lambda
  • Sort Data.Map by value and get all biggest values
  • Cannot create an instance of an interface (PagedList)
  • Linq distinct based on two columns
  • RavenDB OrderByDescending and Take - Incorrect Results
  • How do i convert a List to List in c#
  • Using Regex to split XML string before and after match
  • Linq to SQL left outer join using Lambda syntax and joining on 2 columns (composite join key)
  • ObjectMaterialize in EF not firing on first level query
  • Model binder ValueProvider appends to the existing value + MVC 4
  • Recursion in ASP.NET Core Razor views
  • MVC extension method error
  • How to bind comma separated list of values to List
  • Stitching 2 images (OpenCV)
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • NHibernate manually control fetching
  • XSLT foreach repeating nodes to flat
  • List images(01.png) and descriptions(01.txt) from directory
  • Force show.bind execution
  • D3 nodes and links from JSON with nested arrays of children
  • Can Jackson SerializationFeature be overridden per field or class?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How to push additional view controllers onto NavigationController but keep the TabBar?