如何使用代码隐藏在MVC中禁用Kendo Grid上的编辑?

时间:2022-08-25 23:23:51

In my controller I want to check to see if the due date of a form is in the past and then set the Kendo grid to be view only and not editable. Otherwise I want it to be editable.

在我的控制器中,我想检查表单的截止日期是否已过去,然后将Kendo网格设置为仅查看而不可编辑。否则我希望它是可编辑的。

I could set the Viewbag property in code behind and just do the changes in the client side but I was hoping to do this all server side. Is this possible? I am somewhat new to MVC and I'm frustrated with how this would be so easy with an ASP.NET grid.

我可以在后面的代码中设置Viewbag属性,只是在客户端进行更改,但我希望在服务器端执行此操作。这可能吗?我对MVC有些新手,我对使用ASP.NET网格如何轻松实现感到沮丧。

What's the code behind way to disable a grid in MVC?

在MVC中禁用网格的代码是什么?

I'm also new to Kendo so I'm not quite sure how to even disable editting on a Kendo UI grid client side. So any way possible is fine by me!

我也是剑道新手所以我不太确定如何禁用Kendo UI网格客户端的编辑。所以任何可能的方式对我来说都很好!

1 个解决方案

#1


0  

You can set each column's editable property as below.

您可以设置每列的可编辑属性,如下所示。

Controller:

public ActionResult Index()
    {

        ProductModel model = new ProductModel();
        model.lst_product = rep.ReadAll();
        model.myvar = "no_edit";

        return View(model);
    }

View:

@model MtBarkerApplication.Models.ProductModel

    @(Html.Kendo().Grid((IEnumerable<MtBarkerApplication.Models.ProductModel>)Model.lst_product)    
        .Name("grid")
        .Columns(columns =>
        {

            columns.Bound(o => o.ProductID).Visible(false);
            columns.Bound(o => o.ProductCode).Title("Product Code");
            columns.Bound(o => o.ProductDescription).Title("Product Description");
            columns.Command(command => { command.Edit(); command.Destroy(); }).Width(182);
        })
        .ToolBar(toolbar => toolbar.Create())
        .Editable(editable => editable.Mode(GridEditMode.InLine))
        .Pageable()
        .Sortable()
        .Filterable()
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(50)
            .Events(events => events.Error("error_handler"))
            .Model(model => model.Id(p => p.ProductID))
            .Create(update => update.Action("EditingInline_Create", "Product"))
            .Read(read => read.Action("EditingInline_Read", "Product"))
            .Update(update => update.Action("EditingInline_Update", "Product"))
            .Destroy(update => update.Action("EditingInline_Destroy", "Product"))
           .Model(model =>
            {
                if (Model.myvar == "no_edit")
                {
                    model.Field(p => p.ProductCode).Editable(false);
                }

          })
        )
    )

#1


0  

You can set each column's editable property as below.

您可以设置每列的可编辑属性,如下所示。

Controller:

public ActionResult Index()
    {

        ProductModel model = new ProductModel();
        model.lst_product = rep.ReadAll();
        model.myvar = "no_edit";

        return View(model);
    }

View:

@model MtBarkerApplication.Models.ProductModel

    @(Html.Kendo().Grid((IEnumerable<MtBarkerApplication.Models.ProductModel>)Model.lst_product)    
        .Name("grid")
        .Columns(columns =>
        {

            columns.Bound(o => o.ProductID).Visible(false);
            columns.Bound(o => o.ProductCode).Title("Product Code");
            columns.Bound(o => o.ProductDescription).Title("Product Description");
            columns.Command(command => { command.Edit(); command.Destroy(); }).Width(182);
        })
        .ToolBar(toolbar => toolbar.Create())
        .Editable(editable => editable.Mode(GridEditMode.InLine))
        .Pageable()
        .Sortable()
        .Filterable()
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(50)
            .Events(events => events.Error("error_handler"))
            .Model(model => model.Id(p => p.ProductID))
            .Create(update => update.Action("EditingInline_Create", "Product"))
            .Read(read => read.Action("EditingInline_Read", "Product"))
            .Update(update => update.Action("EditingInline_Update", "Product"))
            .Destroy(update => update.Action("EditingInline_Destroy", "Product"))
           .Model(model =>
            {
                if (Model.myvar == "no_edit")
                {
                    model.Field(p => p.ProductCode).Editable(false);
                }

          })
        )
    )