【问题标题】:Displaying results of a Stored Procedure in MVC View (EF 5 / MVC 4)在 MVC 视图中显示存储过程的结果 (EF 5 / MVC 4)
【发布时间】:2013-06-08 17:19:23
【问题描述】:

目标

我想在我的视图中显示存储过程的结果。

问题

Entity Framework 自动为我导入了一个执行过程的方法,但是我没有得到我期望在屏幕上显示的结果。

导入的函数是:

public virtual ObjectResult<getProductsListForHome_Result> getProductsListForHome(Nullable<int> inOffer, Nullable<int> categoryId)
{
    var inOfferParameter = inOffer.HasValue ?
        new ObjectParameter("inOffer", inOffer) :
        new ObjectParameter("inOffer", typeof(int));

    var categoryIdParameter = categoryId.HasValue ?
        new ObjectParameter("categoryId", categoryId) :
        new ObjectParameter("categoryId", typeof(int));

    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<getProductsListForHome_Result>("getProductsListForHome", inOfferParameter, categoryIdParameter);
}

我已经尝试过的

在 ProductsController 上:

//
// GET: /Products/
public ActionResult Index()
{
    ObjectResult<getProductsListForHome_Result> products = db.getProductsListForHome(1, 14);
    return View(products.ToList());
}

使用前面的代码,当我访问 http://myapp.com/Products/ 时,我收到以下消息:

传入字典的模型项是类型 'System.Collections.Generic.List1[MyApp.Models.getProductsListForHome_Result]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[MyApp.Models.bm_products]'。

我该怎么做才能解决这个问题?

【问题讨论】:

  • 你需要知道的都在错误信息中,你应该能从错误信息中弄清楚。
  • 你好,@DGibbs。感谢您的提示(?),但我是初学者,我仍然不能用自己的脚走路,所以我请求帮助。

标签: c# mysql entity-framework stored-procedures entity-framework-5


【解决方案1】:

第一个,写得很好的问题!

这是一个类型转换问题,看起来你的答案是这里接受的答案:

MVC: The model item passed into the dictionary is of type X, but this dictionary requires a model item of type X

【讨论】:

  • 非常感谢——您的链接对我非常有用,因为它阐明了视图也参与了这个过程。
【解决方案2】:

你的 View 很可能是一个强类型的,它被声明为

@model System.Collections.Generic.IEnumerable<MyApp.Models.bm_products>

但是你在控制器中传递了一个不同的类型,并且遇到了错误。

你可以做什么:

  1. 为视图指定另一种类型。视图本身可能需要在此之后进行一些重构:

    @model System.Collections.Generic.IEnumerable<MyApp.Models.getProductsListForHome_Result>
    
  2. 首选。在控制器中运行一些代码,将 SP 返回的集合转换为 View 可以使用的东西:

    public ActionResult Index()
    {
        ObjectResult<getProductsListForHome_Result> products = db.getProductsListForHome(1, 14);
    
        List<bm_products> viewProducts = products.Select(p => new bm_products{ProductName = p.Name, ProductPrice = p.Price}).ToList();
    
        return View(viewProducts);
    }
    

【讨论】:

  • 哇!差不多了!如何设置属性?
  • @chiefGui,使用语法示例进行编辑。左手是bm_products 类的属性,右手是getProductsListForHome_Result 的属性。查看object initializers in C# 以获得更好的理解。
  • 哦,谢谢@Andrei!只是想问一下,Entity Framework + Stored Procedures 有一些限制,对吧?一种是用于自定义列名,继续吗?我问这个是因为我的程序正在获取表格的最高和最低价格,然后我创建了两列(按程序),称为“minProductPrice”和“maxProductPrice”,它们在表格中不存在。最后一个问题:“p”变量没有找到任何属性。
  • @chiefGui,每段代码都有其局限性。如果我正确理解你的情况,这里没有限制。 EF 使用类模型与 DB 一起工作,因此如果您创建一个具有相应属性的类,或者让 EF 为您生成一个类 - 您可以安全地使用返回不存在列的存储过程。
  • 知道了!那么,如何将“min/maxProductPrice”设置为属性? bm_products 的表中没有这样的列。
猜你喜欢
  • 2016-08-07
  • 2018-01-02
  • 1970-01-01
  • 2020-04-18
  • 2017-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多