不同类型的ViewDataDictionary

时间:2017-05-20 18:00:20

标签: asp.net-mvc linq asp.net-core

在ASP Core 项目中,我有一个客户模型:

using System;
using System.Collections.Generic;

namespace RiskDotNet.Models
{
    public partial class Customers
    {
        public Customers()
        {
            Accounts = new HashSet<Accounts>();
        }

        public string SrcSys { get; set; }
        public string CustId { get; set; }
        public string CustNm { get; set; }

        public virtual ICollection<Accounts> Accounts { get; set; }
    }
}

此后,与每位客户有关的帐户为:

using System;
using System.Collections.Generic;

namespace RiskDotNet.Models
{
    public partial class Accounts
    {
        public Accounts()
        {
            Balances = new HashSet<Balances>();
        }

        public string SrcSys { get; set; }
        public string CustId { get; set; }
        public string AccId { get; set; }
        public string ProdId { get; set; }

        public virtual ICollection<Balances> Balances { get; set; }
        public virtual Customers Customers { get; set; }
    }
}

与每个账户有关的余额(交易)的第三个模型:

using System;
using System.ComponentModel.DataAnnotations;

namespace RiskDotNet.Models
{
    public partial class Balances
    {
        [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}")]
        public DateTime RepDt { get; set; }
        public string CustId { get; set; }
        public string AccId { get; set; }
        public string SrcSys { get; set; }
        public decimal? PrOs { get; set; }

        public virtual Accounts X { get; set; }
    }
}

现在,虽然我想要余额表格,在帐户页面详细信息视图中,但需要按照排序的方式进行反映报告日期的降序(RepDt),我无法这样做。

帐户控制器的以下详细信息部分有什么问题:

    public async Task<IActionResult> Details(string _SrcSys, string _CustId, string _AccId) //All three join up to form the Composite Key
    {
        if (_SrcSys == null || _CustId == null || _AccId == null)
        {
            return NotFound();
        }

        var Accs = await _context.Accounts
            .Include(Cust => Cust.Customers) //To reflect the Customer's Name
            .Include(Bal => Bal.Balances) //The Main Portion I want sorted
            .SingleOrDefaultAsync(m => m.SrcSys == _SrcSys && m.CustId == _CustId && m.AccId == _AccId);
        return View(Accs.Balances.OrderByDescending(x => x.RepDt));
    }

错误返回堆栈:

  

InvalidOperationException:传递给ViewDataDictionary的模型项的类型为&#39; System.Linq.OrderedEnumerable`2 [RiskDotNet.Models.Balances,System.DateTime]&#39;,但此ViewDataDictionary实例需要模型项类型&#39; RiskDotNet.Models.Accounts&#39;。

遵循视图模型:

@model RiskDotNet.Models.Accounts
@{ViewData["Title"] = "Details";}
<div>
        <dt><strong>Account:</strong></dt>
        <dd>@Html.DisplayFor(model => model.AccId)</dd>
        <dt><strong>Src. System:</strong></dt>
        <dd>@Html.DisplayFor(model => model.SrcSys)</dd>
        <dt><strong>Product:</strong></dt>
        <dd>@Html.DisplayFor(model => model.ProdId)</dd>
    </dl>
</div>
<div>
    <dl>
        <dd>
            <table class="table">
                <tr>
                    <th>Date</th>
                    <th style="text-align:right">Pr. O/s.</th>
                </tr>
                @foreach (var item in Model.Balances)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.RepDt)
                        </td>
                        <td style="text-align:right">
                            @Html.DisplayFor(modelItem => item.PrOs)
                        </td>
                    </tr>
                }
            </table>
        </dd>
    </dl>
</div>
    <a asp-action="Index">Accounts' List</a>

0 个答案:

没有答案