无法访问asp.net mvc中foreach循环中的存储过程值

时间:2017-02-22 07:03:48

标签: asp.net-mvc stored-procedures

无法访问foreach循环中存储过程的SQL select语句的列值 运行代码时出现此错误 enter image description here

这是存储过程

ALTER proc [dbo].[getAuthorBooks]
@id int
as
Begin
select BookName,Paid from Book where AuthorId=@id
End

这是C#代码  public ActionResult Edit(int id)         {

        Author auth = new Author();
        parhowDBEntities d=new parhowDBEntities();
        var data = d.getAuthorBooks(9).ToList();
        int x=data.Count;
        ViewBag.AB = data;

        Author auth2 = new Author();

        auth2 = auth.GetEntity(id);

        return View(auth2);
    }

这是查看代码

        @foreach (var item in ViewBag.AB)
        {
            @item.BookName
        }

1 个答案:

答案 0 :(得分:0)

首先,您应该将ViewBag.ViewBag.AB强制转换为List。然后您可以访问如下列:

@foreach (var item in  (List<Author>)ViewBag.AB )
        {
            <td> @item.BookName </td>
        }