在MVC局部视图中显示webgrid时出现问题

时间:2011-09-14 23:06:36

标签: asp.net-mvc asp.net-mvc-3 webgrid asp.net-mvc-partialview

我已经有了一个观点。现在我必须在该视图中添加一个新的网格。 为此,我创建了一个局部视图,并尝试在部分中绑定webgrid但没有成功。 :(

这是我的控制者:

public ActionResult RegisteredUsers()
    {
        var query = from p in dc.UserProfile
                    select new RegisterModel()
                    {
                        UserName = p.Name
                    };

        List<RegisterModel> users = query.ToList();

        return View(users);
    }

这是我的部分观点:

<%
    var grid = new WebGrid(Model,rowsPerPage:5);
    %>
    <%: 
       grid.GetHtml(
                    tableStyle: "wGrid",
                    headerStyle: "wGridHeader",
                    alternatingRowStyle: "alt",
                    footerStyle: "wGridFooter",
                    columns: grid.Columns(

                    grid.Column("UserName","User name")

                  )
               )
    %>

但是,它在我的局部视图中显示了webgrid中的错误。错误是: 必须先绑定数据源才能执行此操作。

错误是不言自明的。但是“模型”中的行“var grid = new WebGrid(Model,rowsPerPage:5);”是数据源。不是吗? 伙计们知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我不确定aspx的方式,但是在部分视图的顶部使用Razor,你必须声明像@model <namespace of model>.<modelname>这样的东西,因为你传递一个列表然后你需要@model IEnumerable<namespace of model>.RegisterModel