将KeyFieldName从gridview绑定到列设置中的Url.Action

时间:2014-04-15 08:43:12

标签: c# asp.net asp.net-mvc gridview devexpress

我试图将对象键值字段值设置为列设置中的RouteValueDictionary,但由于某种原因,我只能访问绑定到此调用内的列的字段!

代码:

    @Html.DevExpress().GridView(settings =>
        {
            //General settings
            settings.Name = "name";
            settings.KeyFieldName = "ID";
            //Other settings etc.....

            settings.Columns.Add(column =>
            {
                column.Name = "Name";
                column.FieldName = "CurrentFieldBoundToColumn";
                column.Caption = "Some caption";

                //Set hyperlink
                column.ColumnType = MVCxGridViewColumnType.HyperLink;
                var hyperLinkProperties = column.PropertiesEdit as HyperLinkProperties;
                String urlFormatString = Url.Action("Action", "Controller", new RouteValueDictionary(new { ID= "{0}" }));
                hyperLinkProperties.NavigateUrlFormatString = HttpUtility.UrlDecode(urlFormatString);


            });
    //Other Columns etc etc
 });

{ID =" {0}" }))这是我需要Keyfield值,但无论我尝试什么,我只得到绑定到列column.FieldName =" CurrentFieldBoundToColumn"。 Anny想法?

EDIT;

得到它像这样:

settings.Columns.Add(column =>
{
    column.Name = "Name";
    column.FieldName = "CurrentFieldBoundToColumn"";
    column.Caption = "SomeCaption";

    //Set hyperlink
    column.SetDataItemTemplateContent(content =>
    ViewContext.Writer.Write
    (
        Html.ActionLink
        (
            linkText: content.Text, 
            actionName: "Action", 
            controllerName: "Controller",
            routeValues: new { ID = content.KeyValue }, 
            htmlAttributes: null
        )
    ));
});

2 个答案:

答案 0 :(得分:4)

您可以使用SetDataItemTemplateContent方法使用简单的ActionLinkGridViewBaseRowTemplateContainer.KeyValue属性:

@Html.DevExpress().GridView(settings =>
{
    //General settings
    settings.Name = "name";
    settings.KeyFieldName = "ID";
    //Other settings etc.....

    settings.Columns.Add(column =>
    {
        column.Name = "Name";
        column.FieldName = "CurrentFieldBoundToColumn";
        column.Caption = "Some caption";

        column.SetDataItemTemplateContent(content =>
            ViewContext.Writer.Write(
                Html.ActionLink(content.Text, "Action", "Controller",
                new { ID = content.KeyValue }, null)));
    });
    //Other Columns etc etc
});

答案 1 :(得分:0)

    settings.Columns.Add(c =>
    {
        c.FieldName = "Dept_Name"; c.Caption = "Dept Name";
        c.SetDataItemTemplateContent(i =>
        {
            ViewContext.Writer.Write(Html.ActionLink(DataBinder.Eval(i.DataItem, "Dept_Name").ToString(), "Edit", new { Dept_Id = DataBinder.Eval(i.DataItem, "Dept_Id").ToString().Trim() }).ToHtmlString());
        });
    });