Home > front end >  How would I use @Html.DisplayNameFor with a ViewModel with two models?
How would I use @Html.DisplayNameFor with a ViewModel with two models?

Time:12-07

I can't figure out how to use Html.DisplayNameFor with the following

My Index.cshtml

@using MultipleModelInOneView;
@model  ViewModel
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div >
    <div >
        <div >
            <div >
                <div >
                    <div >
                        <div >
                            <h4 >Customer Enquiry Products</h4>
                            <p ></p>
                            <!-- Button trigger modal -->
                            <button type="button"  data-toggle="modal" data-target="#exampleModal">
                                Open Create Modal
                            </button>

                            <div id="mymodal" >
                                @Html.Partial("~/Views/Shared/_CustomerEnquiryCreate.cshtml", this.Model.CustomerEnquiry)
                            </div>


                            <table id="datatable-buttons"  style="border-collapse: collapse; border-spacing: 0; width: 100%;">
                                <thead>
                                    <tr>
                                        <th>
                                            @Html.DisplayNameFor(model => model.TargetPrice)
                                        </th>
                                        <th>
                                            @Html.DisplayNameFor(model => model.Quantity)
                                        </th>
                                        <th>
                                            @Html.DisplayNameFor(model => model.LeadTime)
                                        </th>
                                        <th>
                                            @Html.DisplayNameFor(model => model.Category)
                                        </th>
                                        <th>
                                            @Html.DisplayNameFor(model => model.RoHSStatus)
                                        </th>
                                        <th>
                                            @Html.DisplayNameFor(model => model.SalesRep)
                                        </th>
                                        <th>
                                            @Html.DisplayNameFor(model => model.PackageCase)
                                        </th>
                                        <th>
                                            @Html.DisplayNameFor(model => model.Description)
                                        </th>
                                        <th>
                                            @Html.DisplayNameFor(model => model.Currency.Currency1)
                                        </th>
                                        <th>
                                            @Html.DisplayNameFor(model => model.CustomerEnquiry.SalesRep)
                                        </th>
                                        <th>
                                            @Html.DisplayNameFor(model => model.Product.PartNumber)
                                        </th>
                                        <th>
                                            @Html.DisplayNameFor(model => model.RFQManufactuer.Name)
                                        </th>
                                        <th></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var item in Model)
                                    {
                                        <tr>
                                            <td>
                                                @Html.DisplayFor(modelItem => item.TargetPrice)
                                            </td>
                                            <td>
                                                @Html.DisplayFor(modelItem => item.Quantity)
                                            </td>
                                            <td>
                                                @Html.DisplayFor(modelItem => item.LeadTime)
                                            </td>
                                            <td>
                                                @Html.DisplayFor(modelItem => item.Category)
                                            </td>
                                            <td>
                                                @Html.DisplayFor(modelItem => item.RoHSStatus)
                                            </td>
                                            <td>
                                                @Html.DisplayFor(modelItem => item.SalesRep)
                                            </td>
                                            <td>
                                                @Html.DisplayFor(modelItem => item.PackageCase)
                                            </td>
                                            <td>
                                                @Html.DisplayFor(modelItem => item.Description)
                                            </td>
                                            <td>
                                                @Html.DisplayFor(modelItem => item.Currency.Currency1)
                                            </td>
                                            <td>
                                                @Html.DisplayFor(modelItem => item.CustomerEnquiry.SalesRep)
                                            </td>
                                            <td>
                                                @Html.DisplayFor(modelItem => item.Product.PartNumber)
                                            </td>
                                            <td>
                                                @Html.DisplayFor(modelItem => item.RFQManufactuer.Name)
                                            </td>
                                            <td>
                                                @Html.ActionLink("Edit", "Edit", new { id = item.EnqProdID }) |
                                                @Html.ActionLink("Details", "Details", new { id = item.EnqProdID }) |
                                                @Html.ActionLink("Delete", "Delete", new { id = item.EnqProdID })
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <!-- end col -->
            </div>
            <!-- end row -->
        </div>
        <!-- end main content-->
    </div>
    <!-- END layout-wrapper -->
</div>

using REScrmNov22.Data;
using REScrmNov22.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace MultipleModelInOneView
{
    public class ViewModel
    {
        public IEnumerable<CustEnquiryProduct> CustEnquiryProduct { get; set; }
        public IEnumerable<CustomerEnquiry> CustomerEnquiry { get; set; }
    }

}

Action Result of the Index

        public ActionResult Index()
        {
            var custEnquiryProducts = db.CustEnquiryProducts.Include(c => c.Currency).Include(c => c.CustomerEnquiry).Include(c => c.Product).Include(c => c.RFQManufactuer);

            var customerEnquiries = db.CustomerEnquiries.Include(c => c.Customer).Include(c => c.EnquiryStatu);

            ViewModel mymodel = new ViewModel();
            mymodel.CustEnquiryProduct = custEnquiryProducts.ToList();
            mymodel.CustomerEnquiry = customerEnquiries.ToList();

            return View(mymodel.CustEnquiryProduct);
        }

I have been bashing my head against this for about 2 hours and I know I'm probably mising something simple but unsure what it is, as such i have tired loads of stuff but im still new to MVC so not sure if its something I overlooked.

CodePudding user response:

Shouldnt you just return the model, e.g.:

return View(mymodel);

And then, reference each object as needed, for example:

<th>
    @Html.DisplayNameFor(model => model.CustEnquiryProduct.TargetPrice)
</th>

CodePudding user response:

It seems that you wanna use @Html.DisplayNameFor(xx) in a nested model, So you can refer below code:

return View(mymodel);

Then:

@Html.DisplayNameFor(model=>model.CustomerEnquiry.First().SalesRep)

@Html.DisplayNameFor(model=>model.CustEnquiryProduct.First().xxxxx)

Now, The view will show the name of this property.

  • Related