DevExpress MVC 网格视图

DevExpress MVC Gridview

大家好 我正在尝试使用此代码在网格视图上添加一个额外的列。但是当我运行它。我无法单击网格视图上的按钮。知道我做错了什么。谢谢

        settings.Columns.Add(column =>
        {
            column.FieldName = "Unbound";
            column.Caption = "Action";
            column.UnboundType = DevExpress.Data.UnboundColumnType.Object
            column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.True;
            column.ReadOnly = false;
            column.SetDataItemTemplateContent((c) =>
            {

                Html.DevExpress().Button(b =>
                {
                    b.Name = "btnVE" + c.KeyValue;
                    b.Text = "V/E";
                    b.ClientSideEvents.Click =
                    "function(s, e) { document.location='" + DevExpressHelper.GetUrl(new { Controller = "ViewPrincipal", Action = "EditRecord" })
                    + "?key=' + s.GetRowKey(e.visibleIndex); }";

                }).GetHtml();
            });
        });

        settings.Columns.Add("Id");
        settings.Columns.Add("Code");
        settings.Columns.Add("CompanyId");
        settings.Columns.Add("Description");
        settings.Columns.Add("ContactPerson");
        settings.Columns.Add("TelNo");
        settings.Columns.Add("Notes");

更新: 我通过网络开发人员工具在网络上发现了错误,但我不知道如何修复它 Error button hover

听起来问题出在 s,它分配给了此块上的按钮发送器而不是 GridView 行:

Html.DevExpress().Button(b =>
{
    b.Name = "btnVE" + c.KeyValue;
    b.Text = "V/E";
    b.ClientSideEvents.Click =
    "function(s, e) { document.location='" + DevExpressHelper.GetUrl(new { Controller = "ViewPrincipal", Action = "EditRecord" })
    + "?key=' + s.GetRowKey(e.visibleIndex); }"; // ==> 's' refers to button object as sender
}).GetHtml();

你应该使用 GridViewDataItemTemplateContainer 对象来获取相应行的 KeyValue 属性,returns 来自 GridViewBaseRowTemplateContainer:[=24 的整数值=]

column.SetDataItemTemplateContent((c) =>
{
    Html.DevExpress().Button(b =>
    {
        b.Name = "btnVE" + c.KeyValue;
        b.Text = "V/E";
        b.UseSubmitBehavior = false; // prevent default submit action
        b.EnableClientSideAPI = true; // add this line if not sure
        b.ClientSideEvents.Click =
        "function(s, e) { window.location = '" + DevExpressHelper.GetUrl(new { Controller = "ViewPrincipal", Action = "EditRecord" })
        + "?key=" + c.KeyValue.ToString() + "'; }";

    }).GetHtml();
});

或使用更易读的string.Format()

b.ClientSideEvents.Click = string.Format("function(s, e) {{ window.location = '{0}?key={1}'; }}", 
                           DevExpressHelper.GetUrl(new { Controller = "ViewPrincipal", Action = "EditRecord" }), 
                           c.KeyValue.ToString());

备注:

1) 如果要获取行索引,使用c.VisibleIndex.

2) 对于跨浏览器问题,我更喜欢 window.location 而不是 document.location,因为 here.

相关问题:GridView - How to define Button inside grid

找到问题了。显然这应该在创建网格视图之前放在视图的前面。但我认为这应该是一个 JScript。它应该 运行 异步。哦,只要它有效。感谢@tetsuya

的帮助
 @Html.DevExpress().GetStyleSheets(
        new StyleSheet { ExtensionSuite = ExtensionSuite.NavigationAndLayout },
        new StyleSheet { ExtensionSuite = ExtensionSuite.Editors, ExtensionType = ExtensionType.Button },
        new StyleSheet { ExtensionSuite = ExtensionSuite.GridView }

    )
    @Html.DevExpress().GetScripts(
        new Script { ExtensionSuite = ExtensionSuite.NavigationAndLayout },
        new Script { ExtensionSuite = ExtensionSuite.Editors, ExtensionType = ExtensionType.Button },
        new Script { ExtensionSuite = ExtensionSuite.GridView }
    )