0

我是 ASP.net MVC 3.0 的新手,我想将我的数据显示到 WebGrid,并在视图中编写此代码。

@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>

这是控制器

private MovieEntities db = new MovieEntities();

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

之后我总是得到错误“查询的结果不能被枚举多次”。我能做些什么来解决它?

4

1 回答 1

0

您传递的模型应该是实现“IEnumerable”的集合。例如,如果您将List其用作集合,请尝试使用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>

与网络资源不同,您可以直接放置您想要显示的任何标签,而不是使用资源。例如"serial No",而不是@MyResource.grid_SlNo

根据您的评论添加了修改。

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"} ..}

通过这样的模型

于 2012-10-10T10:28:05.007 回答