Reputation: 9
Can't Access column values of SQL select statement of Stored Procedure in foreach loop
This Error occure when run code
enter image description here
This is stored Procedure
ALTER proc [dbo].[getAuthorBooks]
@id int
as
Begin
select BookName,Paid from Book where AuthorId=@id
End
This is C# code 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);
}
This is View Code
@foreach (var item in ViewBag.AB)
{
@item.BookName
}
Upvotes: 0
Views: 422
Reputation: 2245
The first, you should cast ViewBag.ViewBag.AB to List. And then you can access column like below:
@foreach (var item in (List<Author>)ViewBag.AB )
{
<td> @item.BookName </td>
}
Upvotes: 0