Asp.net mvc 不同类型的ViewDataDictionary

Asp.net mvc 不同类型的ViewDataDictionary,asp.net-mvc,linq,asp.net-core,Asp.net Mvc,Linq,Asp.net Core,在ASP核心项目中,我为客户提供了一个模型: using System; using System.Collections.Generic; namespace RiskDotNet.Models { public partial class Customers { public Customers() { Accounts = new HashSet<Accounts>(); }

在ASP核心项目中,我为客户提供了一个模型:

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; }
    }
}
现在,虽然我希望账户页面详细信息视图中的余额表能够以排序方式反映出来,就像报告日期(RepDt)的降序一样,但我无法做到这一点

Accounts Controller的以下详细信息部分有什么问题:

    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));
    }
public async Task Details(string _SrcSys,string _CustId,string _AccId)//所有三个组合起来形成复合键
{
如果(_SrcSys==null | | | u CustId==null | | | u AccId==null)
{
返回NotFound();
}
var Accs=wait_context.Accounts
.Include(Cust=>Cust.Customers)//以反映客户的名称
.Include(余额=>Bal.Balances)//我要排序的主要部分
.SingleOrDefaultAsync(m=>m.SrcSys==\u SrcSys&&m.CustId==\u CustId&&m.AccId==\u AccId);
返回视图(Accs.Balances.OrderByDescending(x=>x.RepDt));
}
错误返回堆栈:

InvalidOperationException:传递到ViewDataDictionary的模型项的类型为“System.Linq.OrderedEnumerable”“2[RiskDotNet.Models.Balances,System.DateTime]”,但此ViewDataDictionary实例需要类型为“RiskDotNet.Models.Accounts”的模型项

遵循视图模型:

@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>
@model RiskDotNet.Models.Accounts
@{ViewData[“Title”]=“Details”;}
账户:
@DisplayFor(model=>model.AccId)
Src。系统:
@DisplayFor(model=>model.SrcSys)
产品:
@DisplayFor(model=>model.ProdId)
日期
公关部。
@foreach(模型余额中的var项目)
{
@DisplayFor(modelItem=>item.RepDt)
@DisplayFor(modeleItem=>item.PrOs)
}
客户名单

您没有显示您的视图,但它显然有
@模型帐户
,但您将
余额集合
传递给它。它们必须是一样的。我已经添加了模型。毫无疑问,我使用了@model。但是引用的源代码没有帮助,我无法捕捉错误。请看你是否能帮忙。主要目的是在账户的详细信息页面上以排序方式显示余额列表。而简单返回视图(Accs);很好,再仔细读一遍!您没有将
账户的集合
传递给视图-您将
余额的集合
传递给了Stephene,毫无疑问,它的余额会传递给账户视图,但是,包括相关余额条目并对其进行排序的正确LINQ是什么?我怀疑您可能想要类似
返回视图的内容(Accs.OrderBy(x=>x.Balances.OrderBy(y=>y.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));
    }
@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>