【问题标题】:How To Show StoredProcedure Result in MVC View如何在 MVC 视图中显示存储过程结果
【发布时间】: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


【解决方案1】:

做类似的事情,这不是确切的解决方案,但请遵循此思路。 1) 定义您的 Viemodel 以存储来自 StoredProc 的数据。

AddressModel.cs

public class AddressModel
{
    public string City {get; set;}
    public string State {get; set;}
}

AddressSearchResultModel.cs

public class AddressSearchResultModel
{
    public List<AddressModel> AddressResults {get; set;}
}

2) 从storedproc 获取数据后,将数据传递给定义的模型。

var dataFromStoredProc=db.Search(Country, state, city, str);
// Translate the addressSerachResultModel to addressSearchResultModel
var addressSearchResultModel= new AddressSearchResultModel();
var addressModel= new AddressModel()
foreach(var item in dataFromStoredProc )
{
    //Assign every property from the dataFromStoredProc to     AddressModel
    //and add it to the list
    addressSearchResultModel.Add(AddressModel);
}

3) 最后从你的视图模型渲染视图

[HttpGet]
public ActionResult GetResults()
{
    var dataFromStoredProc=db.Search(Country, state, city, str);

    // Translate the addressSerachResultModel to AddressSearchResultModel
    var addressSearchResultModel= new AddressSearchResultModel();
    var addressModel= new AddressModel()
    foreach(var item in dataFromStoredProc )
    {
        //Assing every property from the dataFromStoredProc to AddressModel
        //and add it to the list
        addressSearchResultModel.Add(AddressModel);
    }

    return View(addressSearchResultModel);       
}

【讨论】:

    猜你喜欢
    • 2018-01-02
    • 2013-06-08
    • 2017-06-09
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 1970-01-01
    相关资源
    最近更新 更多