【发布时间】:2016-08-07 09:20:14
【问题描述】:
我无法从数据库中获取存储过程结果并显示在视图中。在 DB Storedprocedure 中工作正常
我在 MsSQL 中的存储过程
ALTER PROCEDURE [dbo].[Search]
(
@param1 varchar(max),
@param2 varchar(max),
@param3 varchar(max),
@param4 varchar(max)
)
AS
BEGIN
select BusinessLogo,BusinessTitle,Details,ProfileUrl,Location,Country from ClientBusinesses where location like '%'+@param1+'%' and location like '%'+@param2+'%'
and location like '%'+@param3+'%' and location like '%'+@param4+'%'
END
我在哪里执行 StoredProcedure 的控制器代码
我尝试使用以下代码,但面对但在 ViewBag 中看不到数据它在 ViewBag 上的 Foreach 循环中出现错误,即 BusinessLogo 列不存在,但您可以在 sp 中看到我正在获取 BusinessLogo 以及其他列的类似错误
ViewBag listdata = db.Search(Country, state, city, str);
我也试过下面的代码 -
List<ClientBusiness> listd=new List<ClientBusiness>();
listd=db.Search(Country, state, city, str);
但它给出了错误
Error 2 Cannot implicitly convert type 'System.Data.Entity.Core.Objects.ObjectResult<string>' to 'System.Collections.Generic.List<PromoteMyName.ClientBusiness>' F:\Desktop DATA\Desktop 23 Feb\PromoteMyName\PromoteMyName\Controllers\HomeController.cs 135 23 PromoteMyName
在视图中我正在使用下面的简单代码
@foreach(var item in ViewBag.listdata)
{
}
这是否可以通过 viewbag 将从 sp 获取的数据传递给 view ?
【问题讨论】:
-
您需要将数据投影到模型中并将该模型的集合传递给视图。
-
@StephenMuecke 你说的是视图模型吗
-
我正在使用 dbfirst 方法。 ClientBusiness 是我的表。如果我在 Storedprocedure 中使用 Select *,我可以通过 clientbusiness 模式来查看
-
这是否可以将从 sp 获取的数据通过 viewbag 传递给查看
-
是的,在视图中创建一个具有所需属性的视图模型,并将该模型的集合传递给视图。
标签: c# sql-server asp.net-mvc entity-framework stored-procedures