将数据从视图模型传递到视图问题 ASP.NET MVC

Passing data from viewmodel to view issue ASP.NET MVC

在我的应用程序中,为了创建一个报表视图,我创建了一个 ViewModel:

namespace Asp_PASMVC.ViewModels
{
    [NotMapped]
    public class ReportViewModel
    {
        public string RequestBy { get; set; } // AppRequest
        public string RequestByDessignation { get; set; } // AppRequest
        public string RequestByDepartment { get; set; }// AppRequest
        public string RequestedDate { get; set; }// AppRequest
        public string ApprovalId { get; set; }// AppRequest      
        public string CompanyName { get; set; }
        public List<ApprovalList> ApprovalList { get; set; }
        public List<GeneralItemsApprovedList> GeneralItemsApprovedList { get; set; }
    }

    [NotMapped]
    public class ApprovalList
    {
        public string ApproveDate { get; set; }
        public string ApprovedBy { get; set; }
        public string ApprovedByDessignation { get; set; }
    }

    [NotMapped]
    public class GeneralItemsApprovedList
    {
        public string ItemDescription { get; set; }
        public string ItemPrice { get; set; }
        public byte[] Attachment { get; set; }       
    }
}

然后在控制器中,我加入了我想要获取数据的每个 table 并将该数据传递给报表视图模型。

public ActionResult Index(int id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    AppRequest appRequest = db.AppRequest.Find(id);

    if (appRequest.General.Count != 0)
    {
        List<ReportViewModel> RequestView = new List<ReportViewModel>();


        var ReqDetails = (from a in db.AppRequest
                          join e in db.CreateEmployee on a.Req_By equals e.Id
                          join c in db.CreateCompany on e.CompanyId equals c.Id
                          join d in db.CreateDesignation on e.DesignId equals d.Id
                          join dep in db.CreateDepartment on e.DepId equals dep.Id
                          where a.Id == id
                          select new ReportViewModel
                          {
                              RequestedDate = a.Req_Date.ToString(),
                              RequestBy = e.EmpName,
                              CompanyName = c.CompanyName,
                              RequestByDessignation = d.Designation,
                              RequestByDepartment = dep.Department,
                              GeneralItemsApprovedList= (from a in db.AppRequest
                                                         join g in db.General on a.Id equals g.Req_Id
                                                         join gi in db.GeneralItms on g.Id equals gi.General_Id
                                                         where a.Id == id
                                                         select new GeneralItemsApprovedList
                                                         {
                                                             Attachment = gi.Attachment,
                                                             ItemDescription = gi.Attachment_Description,
                                                             ItemPrice = gi.Attachment_Amount.ToString()


                                                         }).ToList(),

                              ApprovalList = (from a in db.AppRequest
                                              join ap in db.ApprovalProcess on a.Id equals ap.Req_Id
                                              join pa in db.ApproveParties on ap.Id equals pa.ApprovalProcess_Id
                                              join ae in db.CreateEmployee on pa.Approver_Id equals ae.Id
                                              join ad in db.CreateDesignation on ae.DesignId equals ad.Id
                                              where a.Id == id
                                              select new ApprovalList
                                              {
                                                  ApproveDate = pa.Approved_Date.ToString(),
                                                  ApprovedBy = ae.EmpName,
                                                  ApprovedByDessignation = ad.Designation,

                                                 

                                              }).ToList()
                                              
                          }).ToList();

        return View("ApprovedReport", ReqDetails);

    }

    return View("");
}

所以当我调用视图的视图模型数据时出现了问题。它没有向我显示我想要调用的属性。请注意,我仍在学习 asp.MVC,同时我开始开发此应用程序用于我的教育目的。所以有些地方我还不清楚。所以不知道我在这里做错了什么。所以这是我传递 ViewModel 数据的视图

@model  IEnumerable <Asp_PASMVC.ViewModels.ReportViewModel>

@{

    Layout = "~/Views/Shared/_Layout.cshtml";
}

<br />
<div class="invoice p-3 mb-3">
    <!-- title row -->
    <div class="row">
        <div class="col-12">
            <h4>
                <i class="fas fa-globe"></i> PAS Approval
                <small class="float-right">@DateTime.Now</small>
            </h4>
        </div>
        <div class="col-12">
            <h4>
            </h4>
        </div>
        <!-- /.col -->
    </div>
    <!-- info row -->
    <div class="row invoice-info">
        <div class="col-sm-4 invoice-col">

            From
            <address>
                <strong> @Html.DisplayFor(model => model.RequestBy)</strong><br> @Html.DisplayFor(model => model.RequestByDessignation)<br> @Html.DisplayFor(model => model.RequestByDepartment)<br> @Html.DisplayFor(model => model.CompanyName)<br><br>@Html.DisplayFor(model => model.RequestedDate)<br>
            </address>
        </div>
        <!-- /.col -->
        <div class="col-sm-4 invoice-col">
            Approved By
            @foreach (var item in Model.ApprovalList)
            {
                <address>
                    <strong>@Html.DisplayFor(model => item.ApprovedBy)</strong><br>@Html.DisplayFor(model => item.ApprovedByDessignation)<br>@Html.DisplayFor(model => item.ApproveDate)<br>@*Approved Date*@<br>
                </address>
            }

错误说:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Asp_PASMVC.ViewModels.ReportViewModel]', but this dictionary requires a model item of type 'Asp_PASMVC.ViewModels.ReportViewModel'.

同样在 @Html.DisplayFor(model => model.RequestBy) 行中,它说 RequestBy 找不到。

您的模型是一个集合:

@model IEnumerable<Asp_PASMVC.ViewModels.ReportViewModel>

因此,要访问集合中的任何项目,必须对其进行迭代。

您可以像下面这样执行:

<div class="col-sm-4 invoice-col">
    From
@foreach (var model in Model)
{
    <address>
        <strong> @Html.DisplayFor(m => model.RequestBy)</strong><br> @Html.DisplayFor(m => model.RequestByDessignation)<br> @Html.DisplayFor(m => model.RequestByDepartment)<br> @Html.DisplayFor(m => model.CompanyName)<br><br>@Html.DisplayFor(m => model.RequestedDate)<br>
    </address>
}
</div>

编辑:

此版本使用嵌套循环显示每个模型项目的 ApprovalList 列表:

<div class="col-sm-4 invoice-col">
    From
@foreach (var model in Model)
{
    <address>
        <strong> @Html.DisplayFor(m => model.RequestBy)</strong><br> @Html.DisplayFor(m => model.RequestByDessignation)<br> @Html.DisplayFor(m => model.RequestByDepartment)<br> @Html.DisplayFor(m => model.CompanyName)<br><br>@Html.DisplayFor(m => model.RequestedDate)<br>
    </address>

</div>

<div class="col-sm-4 invoice-col">
            Approved By
    @foreach (var item in model.ApprovalList)
    {
        <address>
            <strong>@Html.DisplayFor(m => item.ApprovedBy)</strong><br>@Html.DisplayFor(m => item.ApprovedByDessignation)<br>@Html.DisplayFor(m => item.ApproveDate)<br>@*Approved Date*@<br>
        </address>
    }
</div>
}