【问题标题】:How to bind a table without(edit,delete,create) in mvc contrib grid如何在 mvc contrib 网格中绑定没有(编辑、删除、创建)的表
【发布时间】:2010-11-17 01:18:44
【问题描述】:

我在 mvc 2.0 中使用过 mvccontrib 网格 我只想在gridview中绑定一个表……到目前为止我的编码是……我不知道它是对还是错……

Homecontroller.cs:

public ActionResult List(int? page)
{
    using (ProductDataContext db = new ProductDataContext())
    {
        ViewData["product"] = db.Products.ToList().AsPagination(page ?? 1, 10);
        return View("product");
    }
}

我还为此创建了列表视图....... 我应该在 Index.aspx 中写什么编码

到目前为止,我使用这种编码它不起作用......

<% Html.Grid((List<Product>)ViewData["product"])
    .Columns(column =>
        {
            column.For(c => c.CategoryID);
            column.For(c => c.SupplierID);

        }).Render(); 
 %>

它显示没有可用的数据。还是有其他编码?

我的问题只是我想在 mvccontrib 网格中绑定表。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-2 mvccontrib


    【解决方案1】:

    如果它显示没有可用数据,则问题是db.Products.ToList().AsPagination(page ?? 1, 10) 只是不返回任何元素(空集合)。至于为什么会发生这种情况,从您提供的信息中无法判断。这在很大程度上取决于此ProductDataContext 的实现以及数据存储中的可用数据。

    话虽如此,我建议您使用强类型视图:

    public ActionResult List(int? page)
    {
        using (ProductDataContext db = new ProductDataContext())
        {
            var products = db.Products.ToList().AsPagination(page ?? 1, 10);
            return View("product", products);
        }
    }
    

    所以你的观点就变成了:

    <%@ Page
        Language="C#" 
        Inherits="System.Web.Mvc.ViewPage<IEnumerable<AppName.Models.Product>>" %>
    <%@ Import Namespace="AppName.Models" %>
    
    <%= Html.Grid<Product>(Model)
            .Columns(column =>
            {
                column.For(c => c.CategoryID);
                column.For(c => c.SupplierID);
            })
    %>
    

    注意视图是如何强类型化到产品集合的。

    简单、简单、强类型。


    更新:

    根据 cmets 部分的要求,这里是向每一行添加 EditDelete 链接的示例:

    <%= Html.Grid<Product>(Model)
            .Columns(column =>
            {
                column.For("TableLinks").Named("");
                column.For(c => c.CategoryID);
                column.For(c => c.SupplierID);
            })
    %>
    

    TableLinks.ascx部分:

    <%@ Control 
        Language="C#" 
        Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.Product>" %>
    <%@ Import Namespace="AppName.Models" %>
    
    <td>
        <%: Html.ActionLink<ProductsController>(c => c.Edit(Model.Id), "Edit") %> |
        <% using (Html.BeginForm<ProductsController>(c => c.Destroy(Model.Id))) { %>
            <%: Html.HttpMethodOverride(HttpVerbs.Delete) %>
            <input type="submit" value="Delete" />
        <% } %>
    </td>
    

    这当然假设您的 ProductsController 中存在以下操作:

    public ActionResult Edit(int id)
    ...
    [HttpDelete]
    public ActionResult Destroy(int id)
    

    我还邀请您查看我写的 sample MVC application,它说明了这些概念。

    【讨论】:

    • 这是如何回答他的问题的?当然,使用强类型视图是个好主意,但最后,你的代码和他的代码不一样吗?如果您分配的不是产品列表,两者都会崩溃。除此之外,你的代码有什么不同吗(我根本没有发现它)?
    • 区别在于Html.Grid((List&lt;Product&gt;)ViewData["product"])Html.Grid&lt;Product&gt;(Model)。使用 ViewData 很丑陋,需要强制转换,并且取决于控制器操作中使用的实际名称。
    • 你可以使用 mvc contrib grid 发送一些示例编辑和删除吗?请我是新手
    • 这里需要jquery吗
    • 在我的示例中未使用任何 javascript。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    相关资源
    最近更新 更多