我有以下演示类(class)
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
但参数Data
Model
的
// 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# - 如何将 Dictionary 绑定(bind)到 ASP.NET MVC 5 Razor 模板引擎中的部分 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38361361/