【问题标题】:How would I use @Html.DisplayNameFor with a ViewModel with two models?我如何将 @Html.DisplayNameFor 与具有两个模型的 ViewModel 一起使用?
【发布时间】:2022-12-18 16:41:53
【问题描述】:

我不知道如何将 Html.DisplayNameFor 与以下内容一起使用

我的索引.cshtml

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

<div class="main-content">
    <div class="page-content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-body">
                            <h4 class="card-title">Customer Enquiry Products</h4>
                            <p class="card-title-desc"></p>
                            <!-- Button trigger modal -->
                            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
                                Open Create Modal
                            </button>

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


                            <table id="datatable-buttons" class="table table-striped table-bordered dt-responsive nowrap" 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; }
    }

}

指数行动结果

        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);
        }

我已经为此苦恼了大约 2 个小时,我知道我可能错过了一些简单的东西但不确定它是什么,因此我已经厌倦了很多东西但我对 MVC 还是新手所以不确定它是否是我忽略的东西.

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-core model-view-controller


    【解决方案1】:

    你不应该只返回模型,例如:

    return View(mymodel);
    

    然后,根据需要引用每个对象,例如:

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

    【讨论】:

    • 不幸的是,它并没有那么简单,因为我得到了这个错误:编译错误描述:在编译服务该请求所需的资源期间发生错误。请查看以下特定错误详细信息并适当修改您的源代码。编译器错误消息:CS1061:“IEnumerable<CustEnquiryProduct>”不包含“TargetPrice”的定义,并且无法找到接受“IEnumerable<CustEnquiryProduct>”类型的第一个参数的可访问扩展方法“TargetPrice”(您是否缺少使用指令或程序集参考?)
    • 所以那不是一个对象,它是一个列表
    • 好的,那么你有什么建议来解决这个问题?
    【解决方案2】:

    看来你想在嵌套模型中使用@Html.DisplayNameFor(xx),所以你可以参考下面的代码:

    return View(mymodel);
    

    然后:

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

    现在,视图将显示此属性的名称。

    【讨论】:

      猜你喜欢
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      相关资源
      最近更新 更多