0

I am new on ASP.net MVC 3.0, I want to display my data to WebGrid and I write this code in the View.

@model IEnumerable<MvcApplication3.Models.MOVIE>


@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
    @ViewBag.abc;
</p>

@Html.ActionLink("Asc Title", "Index", new { sortOrder = "asc" })
@Html.ActionLink("Desc Title", "Index", new { sortOrder = "desc" })

<table>
    <tr>
        <th>
            TITLE
        </th>
        <th>
            PRICE
        </th>
        <th>
            RELEASEDATE
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>            
            @Html.DisplayFor(modelItem => item.TITLE)
        </td>
        <td align="right">
            @Html.DisplayFor(modelItem => item.PRICE)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.RELEASEDATE)
        </td>
        <td>
            @Html.ActionLink("Edit Saya", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID }) |          
            @Html.ActionLink("Select", "MoviedanArtis", new { id = item.ID }) 
        </td>
    </tr>
}

</table>

@{    
    var grid = new WebGrid(Model.ToList(), rowsPerPage: 6);    
}

<div id="grid">
    @grid.GetHtml(tableStyle: "grid", 
    headerStyle: "head",
    alternatingRowStyle: "alt",
    columns: grid.Columns(grid.Column("TITLE"), grid.Column("PRICE"), grid.Column("RELEASEDATE")))
</div>

Here are the controller

private MovieEntities db = new MovieEntities();

        //
        // GET: /Coba/        
        public ViewResult Index()
        {           
            return View(db.MOVIEs.ToList());
        }

After that I always get error "The result of a query cannot be enumerated more than once." What can I do to solve it?

Eddy Setiawan
  • 77
  • 1
  • 7

1 Answers1

0

THe model you are passing should be a collection which implements 'IEnumerable'. For example if you are using List as a collection, try using IList.

@using MyResource = Resources.ComWebResources
@model IList<App.WorksInformation> 
<div id="worksajaxgrid">

@{
  var grid = new WebGrid(Model, rowsPerPage: 6,
 ajaxUpdateContainerId:"worksajaxgrid",
 ajaxUpdateCallback:"ajaxUpdate_Callback");

   @grid.GetHtml(tableStyle: "webGrid",headerStyle: "header", alternatingRowStyle:     "alt", selectedRowStyle: "webgrid-selectedrow",
   columns: grid.Columns(

  grid.Column("SlNo", @MyResource.grid_SlNo, canSort: false, style: "webcolwidthSlNo"),
  grid.Column("WorkOrderId", @MyResource.grid_SCTWorkOrderId, canSort: false),
  grid.Column("Project", @MyResource.lbl_Project, canSort: false),
  grid.Column("WorkOrderNo", @MyResource.grid_WorkOrderNo, canSort: false),
  grid.Column("WorkCode", @MyResource.grid_WorkCode, canSort: false),
  grid.Column("WorkName", @MyResource.grid_WorkName, canSort: false)
 ));
 }
</div>

Instaed of webresources you can place whatever label you want to display directly, instead of using resources. For instance "serial No" instead of @MyResource.grid_SlNo

Modifications added as per your comment.

public class MOVIE
{
   public string TITLE{get;set;}
   public string NAME{get;set;}


}

IList<MOVIE>MOVIEList= new List<MOVIE>{new MOVIE{"Pirates of the Caribbian","PC"}, new MOVIE{"Lord of the Rings","LR"} ..}

pass a model like this

amesh
  • 1,311
  • 3
  • 21
  • 51
  • I have declared @model IEnumerable as IEnumerable but the error still occur – Eddy Setiawan Oct 11 '12 at 03:36
  • what kind of collection Models.MOVIE is? – amesh Oct 11 '12 at 03:49
  • MOVIE is just a model of my tabel consist of TITLE, NAME and RELEASEDATE. So I used IEnumerable like the ASP.net MVC tutorial – Eddy Setiawan Oct 11 '12 at 05:10
  • Just try IList as your model – amesh Oct 11 '12 at 05:48
  • I still got error. My Controller use "db.MOVIEs.ToList()" to the view. If @model IENumerable changed into @model IList, I got this error: The model item passed into the dictionary is of type 'System.Data.Objects.ObjectResult`1[MvcApplication3.Models.MOVIE]', but this dictionary requires a model item of type 'System.Collections.Generic.IList`1[MvcApplication3.Models.MOVIE]'. – Eddy Setiawan Oct 11 '12 at 06:43
  • Can you update your question with your contoller and view code? – amesh Oct 11 '12 at 07:26