Question

Im trying to get Objects keyfield value to a RouteValueDictionary inside column settings, However for some reason i can only access the field bound to the column inside this call!

Code:

    @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}" })) This is where i need the Keyfield value however whatever i try i only get the value bound to the column column.FieldName = "CurrentFieldBoundToColumn". Anny ideas?

EDIT;

Got it woorking like so :

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
        )
    ));
});
Was it helpful?

Solution

You can use SetDataItemTemplateContent method with a simple ActionLink and GridViewBaseRowTemplateContainer.KeyValue property:

@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
});

OTHER TIPS

    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());
        });
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top