【问题标题】:How to dynamically set the <% = %> in MVC Razor如何在 MVC Razor 中动态设置 <% = %>
【发布时间】:2019-07-02 18:52:50
【问题描述】:

我需要在我的剃须刀代码中动态添加一个关键字段名称。

我只是不确定如何正确设置它,所以 data.KeyNameField 是在运行时设置的

我在剃须刀中尝试了 string.format,但我认为它不会起作用,因为 在客户端上设置关键字段很重要

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

为了能够动态传递我的关键字段名称,以便我可以将唯一 ID 添加到我的 URL。

我需要将 .'THE_GUID' 替换为我的键字段名称模型上的属性

【问题讨论】:

    标签: razor model-view-controller


    【解决方案1】:

    答案:

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

    其中 data.{1} 将是关键字段名称,并将将该字段的值绑定为 id=*

    【讨论】:

      猜你喜欢
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多