gpt4 book ai didi

c# - 如何将字典绑定(bind)到 ASP.NET MVC 5 Razor 模板引擎中的局部 View

转载 作者:行者123 更新时间:2023-11-30 20:19:40 25 4
gpt4 key购买 nike

我有以下演示课

public class CustomerReportPresentation
{
public ReportFormat ReportFormat { get; set; }

public List<Dictionary<string, object>> Data { get; set; }
}

在 Controller 中我有以下代码

CustomerReportPresentation customerReport = new CustomerReportPresentation();

customerReport.Data = ReportModel.Get(); // This will return a list like this List<Dictionary<string, object>>

customerReport.ReportFormat = ReportFormat.Tabular;

return View(customerReport);

现在,在我相应的 View 中,我有以下代码

@model Project1.Areas.Test.Presentation.CustomerReportPresentation
@{
ViewBag.Title = "Index";
}

@if (Model.ReportFormat == Project1.Support.ReportsGenerator.Report.Contracts.ReportFormat.Summary)
{
@Html.Partial("~/Support/ReportsGenerator/Views/Summary.cshtml", Model.Data)
}
else
{
@Html.Partial("~/Support/ReportsGenerator/Views/Tabular.cshtml", Model.Data)
}

我正在将列表传递给局部 View 。然后每个分部 View 将以不同的方式显示数据。

这是我的部分观点

@model List<Dictionary<string, Object>>

<ul>
@foreach (var attributes in Model.Data)
{
<li>
@foreach (var attribute in attributes)
{
@attribute.Value; <text> </text>

}
</li>
}
</ul>

但是当我运行我的项目时,我得到了这个错误

 Compiler Error Message: CS0103: The name 'model' does not exist in the current context

我该如何解决这个问题?

最佳答案

您发送Model.Data进入部分然后尝试访问Model.Data在局部。删除 Data从最后Model .奇怪的是智能感知没有警告你 Model.Data不存在于 List<Dictionary<string, Object>>

@model List<Dictionary<string, Object>>

<ul>
@foreach (var attributes in Model)
{
<li>
@foreach (var attribute in attributes)
{
@attribute.Value; <text> </text>

}
</li>
}
</ul>

这意味着您从 View 发送的模型,即部分接收到的模型不是 CustomerReportPresentation但是参数 DataModel

// At the end you sendt Model.Data, that means the Data object is recieved by the Partial
@Html.Partial("~/Support/ReportsGenerator/Views/Summary.cshtml", Model.Data)

关于c# - 如何将字典绑定(bind)到 ASP.NET MVC 5 Razor 模板引擎中的局部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38361361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com