0

When I create payment I want to display ClientName in my payment Index View. I get client name from another table called "Clients"

enter image description here

Payments Model:

public class Payments
{
    [Key]
    public int PaymentsId { get; set; }

    public int ClientsId { get; set; }
    [ForeignKey("ClientsId")]
    public virtual Clients Clients { get; set; }
    public String Paymentnumber { get; set; }
    public DateTime PaymentDate { get; set; }
    public Decimal Amount { get; set; }
    public Decimal Discount { get; set; }
    public String Reference { get; set; }
    public String Bank { get; set; }

}

Payments controller:

// GET: Payments
public ActionResult Index()
{
    return View(db.PaymentsList.ToList());
}

Payment Index View:

@model IEnumerable<localuh.Models.Payments>
....
<table class="table">
<tr>
    <th>@Html.DisplayNameFor(model => model.Paymentnumber)</th>
    <th>@Html.DisplayNameFor(model => model.PaymentDate)</th>
    <th>@Html.DisplayNameFor(model => model.Amount)</th>
    <th> @Html.DisplayNameFor(model => model.Discount)</th>
    <th>@Html.DisplayNameFor(model => model.Reference)</th>
    <th>@Html.DisplayNameFor(model => model.Bank)</th>
    <th></th>
    <th></th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>@Html.DisplayFor(modelItem => item.Paymentnumber)</td>
    <td>@Html.DisplayFor(modelItem => item.PaymentDate)</td>
    <td>@Html.DisplayFor(modelItem => item.Amount)</td>
    <td>@Html.DisplayFor(modelItem => item.Discount)</td>
    <td>@Html.DisplayFor(modelItem => item.Reference)</td>
    <td>@Html.DisplayFor(modelItem => item.Bank)</td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.PaymentsId }) |
        @Html.ActionLink("Details", "Details", new { id=item.PaymentsId }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.PaymentsId })
    </td>
</tr>
}

</table>

So, how can I do that?

Demain
  • 19
  • 5
  • @StephenMuecke I change all suggestions – Demain Feb 12 '16 at 04:02
  • Need a break, but can you show the model for `Payment` (it you have set this up correctly, it should have an `int ClientID` property and a `virtual Client Client` property) and I'll have a look in 30 min. –  Feb 12 '16 at 04:05
  • `Payment` model (the one returned by `db.PaymentsList`), not `PaymentsViewModel` –  Feb 12 '16 at 04:10
  • Just add another column in your table `@Html.DisplayFor(modelItem => item.Clients.Name)` assuming `Name` is the property of `Clients` that you want to display. Side note: Is confusing that you property is named `Clients` (plural) when it relates to a single Client - suggest you consider renaming –  Feb 12 '16 at 04:21
  • It works... Can you explain me what virtual does at this case?, it makes an instance of my clients Id or something like that? – Demain Feb 12 '16 at 04:25
  • Its used by EF for 'lazy loading' of your related properties - refer the answers [here](http://stackoverflow.com/questions/5597760/what-effects-can-the-virtual-keyword-have-in-entity-framework-4-1-poco-code-fi) and [here](http://stackoverflow.com/questions/7738722/entity-framework-4-1-virtual-properties) for a bit more explanation. –  Feb 12 '16 at 04:28
  • @StephenMuecke Excelent, I test more deeper and I saw that select value always be my first value of my dropdown, If I select another one and submit it, it post my first value anyway – Demain Feb 12 '16 at 04:36
  • Sorry, not sure what you mean. If you have another issue, then ask another question (and post the relevant code - the GET and POST methods, the associated view and the model used in the view) –  Feb 12 '16 at 04:45

1 Answers1

0

Your Payments model contains a virtual property for Clients (which will be 'lazy loaded' by EF, so you simply need to add another table column to display the name of the client. Assuming your Clients model has a property public string Name { get; set; }, then it would be

<td>@Html.DisplayFor(modelItem => item.Clients.Name)</td>

Side note: Suggest you rename the property to Client, not Clients which suggests its a collection.