There is the following problem: There is a table of orders, in which there is the ordered product, the buyer, the employee, the price and the quantity. for the product and the buyer, you only need to display the Name property, so there are no problems, but for an employee I want to display the Surname , Name and Patronymic properties

Wrote such code, but it does not display anything at all.

 @model IEnumerable<Bakery.Models.Order> @{ ViewBag.Title = "Список заказов"; } <h2>Список заказов</h2> <p> @Html.ActionLink("Добавить", "Create") </p> <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.Product) </th> <th> @Html.DisplayNameFor(model => model.Customer) </th> <th> @Html.DisplayNameFor(model => model.Employee) </th> <th> @Html.DisplayNameFor(model => model.Count) </th> <th> @Html.DisplayNameFor(model => model.Price) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Product.Name) </td> <td> @Html.DisplayFor(modelItem => item.Customer.Name) </td> <td> @Html.Display(string.Format("{0} {1} {2}", item.Employee.Surname, item.Employee.Name, item.Employee.Patronymic)) </td> <td> @Html.DisplayFor(modelItem => item.Count) </td> <td> @Html.DisplayFor(modelItem => item.Price) </td> <td> @Html.ActionLink("Edit", "Edit", new { id=item.Id }) | @Html.ActionLink("Delete", "Delete", new { id=item.Id }) </td> </tr> } 

Order class itself

 using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Web; namespace Bakery.Models { public class Order { public Order() { } public Order(Employee employee, Product product, Customer customer, Double count, Double price) { Id = Guid.NewGuid(); Employee = employee; Product = product; Customer = customer; Count = count; Price = price; } /// <summary> /// Код /// </summary> [Key] public Guid Id { get; set; } /// <summary> /// Сотрудник /// </summary> [DisplayName("Сотрудник")] [Required] public Employee Employee { get; set; } [System.Web.Mvc.HiddenInput(DisplayValue = false)] public Guid? EmployeeId { get; set; } /// <summary> /// Изделие /// </summary> [DisplayName("Изделие")] [Required] public Product Product { get; set; } [System.Web.Mvc.HiddenInput(DisplayValue = false)] public Guid? ProductId { get; set; } /// <summary> /// Заказчик /// </summary> [DisplayName("Покупатель")] [Required] public Customer Customer { get; set; } [System.Web.Mvc.HiddenInput(DisplayValue = false)] public Guid? CustomerId { get; set; } /// <summary> /// Количество заказанных изделий /// </summary> [DisplayName("Количество изделий (шт.)")] [Required] [Range(1, Double.MaxValue, ErrorMessage = "Количество не может быть меньше 1")] public double Count { get; set; } /// <summary> /// Цена /// </summary> [DisplayName("Цена (руб.)")] [Required] [Range(0.1, Double.MaxValue, ErrorMessage = "Цена не может быть меньше 0.1")] public double Price { get; set; } } } 
  • Of course, it does not display anything, you don’t have a controller - tym32167
  • the controller is there, and it displays everything except for this column - Alexandr
  • one
    so your string is wrong. What should it do? @Html.Display(string.Format("{0} {1} {2}", item.Employee.Surname, item.Employee.Name, item.Employee.Patronymic)) tried to be replaced with @string.Format("{0} {1} {2}", item.Employee.Surname, item.Employee.Name, item.Employee.Patronymic) ? - tym32167
  • @ tym32167, thanks. helped - Alexandr
  • designed the answer - tym32167

1 answer 1

Your string is wrong.

 @Html.Display(string.Format("{0} {1} {2}", item.Employee.Surname, item.Employee.Name, item.Employee.Patronymic)) 

change to

 @string.Format("{0} {1} {2}", item.Employee.Surname, item.Employee.Name, item.Employee.Patronymic)