【问题标题】:ASP.NET MVC passing parameter to @RenderPage("", new{Test = Test} ) gives errorASP.NET MVC 将参数传递给 @RenderPage("", new{Test = Test} ) 给出错误
【发布时间】:2023-03-09 11:36:01
【问题描述】:

我有一个包含 4 个局部视图的仪表板页面。

我只需要将数据作为参数从Index.cshtml 内部提供给局部视图。

这是我的Index.cshtml 代码(页面顶部):

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}

@using CoreLibrary.Extensions
@using System.Data
@{
    Business.ReportBll.DailyParams prms = new Business.ReportBll.DailyParams();
    prms.end = DateTime.Now; 
    prms.start = DateTime.Now.FirstDayOfMonth();  
    var DataDaily= Business.Report.ReportDaily(prms, true);            
    HtmlString baslangicBitisTarSpan = new HtmlString((prms.strdate.HasValue ? "<span style='margin-left:5px'>Başlangıç:"
            + prms.strdate.Value.ToString("dd.MM.yyyy HH:mm")
            + "</span>" : ""));
}

Index.cshtml的中间,给oage参数

<div class="row mt-3">
    <div class="col-md-6 col-12">
            @RenderPage("~/Views/Report/_DashDaily.cshtml",   new { DataDaily = DataDaily })
     
        </div>
        <div class="col-md-6 col-12">
            @RenderPage("~/Views/Report/_DashboardWeek.cshtml")// data will be added here soon
        </div>
    </div>
</div>

在_DashDaily.cshtml的顶部

@using CoreLibrary.Extensions
@using System.Data

@{       
    var k1 = DataDaily;
    var k = @DataDaily;
}

最后的错误是

CS0103:当前上下文中不存在名称“DataDaily”

它真的很简单,但我不知道为什么我不能。你会帮我吗。谢谢

返回数据的类型

【问题讨论】:

  • DataDaily 是模型类吗?
  • 不,它不是模型类。它只是从给 sql 中返回数据
  • DataSet 还是DataTable

标签: c# .net asp.net-mvc


【解决方案1】:

请试试这个

在 _DashDaily.cshtml 的顶部

@using CoreLibrary.Extensions
@using System.Data
@model DataTable//<YOUR Model Class>
@{
   
    var k1 = Model;    
}

【讨论】:

  • 我作为参数提供的 DataDaily 不是模型类。它只是 sql 返回的数据
  • 同意,但必须是 DataSetDataTable
  • 底部添加照片
  • 好的,然后用DataTable检查答案我已经修改了代码。
  • 不,它仍然给出错误
【解决方案2】:

我是这样解决的。

@using CoreLibrary.Extensions
@using System.Data 
@{
   
   var DataDaily = @Page.DataDaily;
}

【讨论】:

    猜你喜欢
    • 2016-10-25
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 2015-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多