I want to display the manager’s full name in @ Html.TextBoxFor, but I don’t know how to do it.

There is a ModelView of this type.

public class SupplierDetailView { public Supplier Supplier { get; set; } public int[] SelectedServicesIds { get; set; } public IEnumerable<Service> Services { get; set; } } 

I bring this data to the pop-up window. One director can have many managers and so on. This is done in my model.

  public class Supplier { [Display(Name = "SupplierID")] [Key] public int SupplierID { set; get; } [Display(Name = "Коммерческое название")] [StringLength(250)] [Column(TypeName = "char")] public string Name { set; get; } public virtual ICollection<Manager> Managers { get; set; } } 

In short, but the principle will be clear to you, I think. And below is translated model manager.

  public class Manager { [Display(Name = "ManagerID")] [Key] public int ManagerID { set; get; } [Display(Name = "Имя")] [StringLength(250)] [Column(TypeName = "char")] public string FirstName { set; get; } [Display(Name = "Фамилия")] [StringLength(200)] [Column(TypeName = "char")] public string SecondName { set; get; } [Display(Name = "Отчество")] [StringLength(200)] [Column(TypeName = "char")] public string MiddleName { set; get; } } 

I collect the data in the model on the controller and, when requested by id, I transfer it to the editing window for a pop-up window.

  [HttpPost] public ActionResult Edit(int id) { var supplier = db.Suppliers.Find(id); var model = new SupplierDetailView { Supplier = supplier }; return PartialView("_Supplier_Edit", model); } 

As a result, the manager is doing so

  <div class="field"> <label>ФИО</label> @foreach (var manager in Model.Supplier.Managers) { @Html.TextBoxFor(m => manager.Name) } </div> <div class="field"> <label>ФИО</label> @foreach (var manager in Model.Supplier.Managers) { @Html.TextBoxFor(m => manager.FirstName) } </div> 

But this is nonsense, how can I combine two fields into one? Glue the data and display?

    1 answer 1

    If you do not want to use the contract model (DTO), then the easiest way to add a property (eg FullName) to the Manager type, decorate it with the Ignore attribute and sew the installation and read logic of the FirstName, SecondName, MiddleName values ​​into its setter and getter, respectively:

     public class Manager { [Display(Name = "ManagerID")] [Key] public int ManagerID { set; get; } [Display(Name = "Имя")] [StringLength(250)] [Column(TypeName = "char")] public string FirstName { set; get; } [Display(Name = "Фамилия")] [StringLength(200)] [Column(TypeName = "char")] public string SecondName { set; get; } [Display(Name = "Отчество")] [StringLength(200)] [Column(TypeName = "char")] public string MiddleName { set; get; } [Ignore] public string FullName { get { return $"{this.SecondName} {this.FirstName} {this.MiddleName}"; } set { var match = Regex.Match(value, "(?<secondname>\w+)\s(?<firstname>\w+)\s(?<middlename>\w+)"); this.FirstName = match.Groups["firstname"].Value; this.SecondName = match.Groups["secondname"].Value; this.MiddleName = match.Groups["middlename"].Value; } } }