如何使用webgrid ASP.net MVC 3.0?

时间:2012-10-10 08:38:34

标签: asp.net-mvc entity-framework webgrid

我是ASP.net MVC 3.0的新手,我想将数据显示给WebGrid,并在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>

这是控制器

private MovieEntities db = new MovieEntities();

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

之后我总是收到错误“查询的结果不能多​​次枚举”。我该怎么做才能解决它?

1 个答案:

答案 0 :(得分: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>

创建了webresources,你可以放置任何你想直接显示的标签,而不是使用资源。 例如"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"} ..}

传递像这样的模型