Get value from an entity via navigation property

Asked

Viewed 161 times

0

I have a question that seems basic, but I’m racking my brain to solve.

How can I get the value of an entity attribute via Properties Navigations?

For example, I have the classes:

public class Budget
{
    public Guid Id {get; set;}
    ... // Outros atributos

    //Propriedade de Relacionamento
    public IEnumerable<BudgetItem> BudgetItems {get; set;}
}

public class BudgetItem
{
    public Guid Id {get; set;}
    public string NameItem {get; set;}
    ... // Outros atributos

    //ForeingKey
    public Guid BudgetId {get; set;}

    //Propriedade de Navegação
    public Budget Budget {get; set;}
}

In my Infra layer I have all the mapping. It works perfect. The CRUD is performed as planned and in this case, only the entity’s "Ids" are persisted in the bank. Nothing out of standard. Turns out, I want to show in my View > Index a list of BudgetItem, each one being related to a Budget.

In normal consultation it is presented like this:

Table in the View

Budgetitem | Budget (Note: the saved Guid is displayed)
Item 1 | a6fa353b-e55b-42f3-92f2-17cecda4725d
Item 2 | c7b90b8e-4fd1-4422-8c11-bb5fbaae7f05

What I need is for you to show it like this:

Budgetitem | Budget
Item 1 | Name of Budget
Item 2 | Name of Budget

In other applications (EF6 and Windows Forms) I just loaded making a simple type query:

public IEnumerable<BudgetItem> GetByBudgetName(budgetName)
{
   return _context.ButgetItem.Where(c => c.Budget.Name.Equals(budgetName));
}

However, in ASP.NET Core 2.0 this does not work. I simply cannot access data from another class/table through browsing properties.

Follows my View:

<table class="table table-striped table-bordered table-hover">
  <thead>
    <tr>
      <th>@Html.DisplayNameFor(model => model.BudgetItem)</th>
      <th>@Html.DisplayNameFor(model => model.BudgetId)</th>
      <th>@Html.DisplayNameFor(model => model.Value)</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    @foreach (var item in(IEnumerable
    <BudgetItemViewModel>)ViewBag.ListBudget) {
      <tr>
        <td>@Html.DisplayFor(modelItem => item.Month)</td>        
        <td>@Html.DisplayFor(modelItem => item.BudgetId)</td>
        <td>@Html.DisplayFor(modelItem => item.Value)</td>
      </tr>
      }
  </tbody>
</table>

I need to replace the line <td>@Html.DisplayFor(modelItem => item.BudgetId)</td> so that instead of showing the "Id" show the description (name) of the corresponding Budget.

Whereas this is a FK, in my class I have the Navigation Property for the same. Soon I thought it would work like this <td>@Html.DisplayFor(modelItem => item.Budget.Name)</td>.

Noble friends could help me with that?
Thanks in advance.

  • 1

    A question, for me this kind of clause does not work at all c => c.Budget.Name.Equals(budgetName), trade in for c => c.Budget.Name == budgetName wouldn’t solve your whole problem?

  • In fact, either the Equals or "==" is the same. My return always comes empty. I tried to do "Join", but returns empty tbm.

  • The Equals apparently is not interpreted by the assembly of the query to me, I do not understand what happens.

  • Thanks for your attention. But independently, is there another way to get the data I need for my View? The class only saves the Budget "Id". I just need to have the Budget value (Name) shown in my View instead of the string saved as "Id".

  • 1

    Buddy, try to do the include, that works like the join, thus: return _context.ButgetItem.Include(p=> p.Budget ).Where(c => c.Budget.Name.Equals(budgetName)); and in the view access the property, I’m by mobile, already I will answer.

  • And what appears when you wear <td>@Html.DisplayFor(modelItem => item.Budget.Name)</td>? Your property item.Budget actually has the reference to the object Budget correct? You can confirm that?

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.