【问题标题】:Display Records List in View Obtained from Stored Procedure MVC-4在从存储过程 MVC-4 获得的视图中显示记录列表
【发布时间】:2017-12-28 13:47:35
【问题描述】:

我正在尝试显示通过存储过程从数据库获取的记录列表,

我无法在 mvc-4 应用程序的视图中列出记录。

这是现有的代码

   public ActionResult Report_OrderAnalysisDetail(string Status)
        {
               var AnalysisDetail= db.Report_OrderAnalysisDetail(0,25,0,null,null,null,null,null,Status,null);

               return View(AnalysisDetail);
        }

这是完整视图:

@model IList<Agorz_MVCApplication.Models.Report_OrderAnalysisDetail_Result>
<div class="content-wrapper" style="min-height: 1000px;">
    <section class="content-header">
        <h1 class="page-header">Order Analysis Detail</h1>
    </section>
    <div id="RenderPartial" name="RenderPartial" style="min-height:950px;important">
        @Html.Partial("_Report_OrderAnalysisDetail")

    </div>
</div>

这是我要在其中迭代记录的部分视图:

@model IList<Agorz_MVCApplication.Models.Report_OrderAnalysisDetail_Result>
  @foreach (var item in Model)
    {
        <tr class="gradeX odd" role="row">

            <td class="center">@item.OrderId</td>
            <td class="center">@if(item.OrderDate!=null){@item.OrderDate.Value.ToShortDateString()}</td>
            <td class="center">@item.ServiceRequested</td>
            <td class="center">@item.ServiceProvider</td>
            <td class="center">@item.Customer</td>
            <td class="center">@item.OrderStatus</td>
            
        </tr>
    }

当我运行代码并加载视图时,会出现以下错误。

“/”应用程序中的服务器错误。

传入字典的模型项是'System.Data.Objects.ObjectResult'1[Agorz_MVCApplication.Models.Report_OrderAnalysisDetail_Result]'类型的,但是这个字典需要'System.Collections.Generic.IList'类型的模型项1[Agorz_MVCApplication.Models.Report_OrderAnalysisDetail_Result]'。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 stored-procedures viewmodel


    【解决方案1】:
    var AnalysisDetail= db.Report_OrderAnalysisDetail(0,25,0,null,null,null,null,null,Status,null).ToList();
    

    在 sp 返回一个对象时在末尾添加 ToList(),并且您在视图中提供了一个 List 类型

    【讨论】:

    • 很高兴它有帮助:)
    猜你喜欢
    • 2013-06-08
    • 1970-01-01
    • 2016-08-07
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 2022-01-15
    • 2018-08-21
    相关资源
    最近更新 更多