tvb108108
tvb108108

Reputation: 408

How to dynamically set the <% = %> in MVC Razor

I need to dynamic add a key field name in my razor code.

I just am not sure how to set it up properly so data.KeyNameField is set at run time

I tried string.format in my razor, but I do not think it will work because the <% %> is important to set the key field on the client

                                    .CellTemplate(
                                            @<text>
                                            <a href="@Html.Raw(url)?id=<%= data.THE_GUID%>">@Model.OpenRecord.OpenRecordButtonText</a>
                                            </text>);
                                        }

To dynamically be able to pass my key field name in so I can add the unique id onto my URL.

I need to replace .'THE_GUID' with my property on my model of the key field name

Upvotes: 1

Views: 33

Answers (1)

tvb108108
tvb108108

Reputation: 408

Answer:

            if (Model.HasOpenRecordButton && Model.OpenRecord != null)
            {
                string urlAction = Url.Action(Model.OpenRecord.OpenRecordButtonAction, Model.OpenRecord.OpenRecordButtonController);

                string rawUrl = string.Format("{0}?id=<%= data.{1} %>", urlAction, Model.KeyFieldName);

                column
                    .Add()
                    .VisibleIndex(Model.OpenRecord.ColumnPositionIndex)
                    .Caption(Model.OpenRecord.Caption)
                    .DataType(GridColumnDataType.String)
                    .Width(Model.OpenRecord.ColumnWidth)
                    .CellTemplate(
                            @<text>
                                <a href="@Html.Raw(rawUrl)">@Model.OpenRecord.OpenRecordButtonText</a>
                            </text>
                    );
            }

Where data.{1} will be the key field name and will BIND the value of that field as being id=*

Upvotes: 1

Related Questions