添加“目标”; Telerik MVC 扩展中选项卡条项目 URL 的属性

发布于 2024-12-09 19:50:19 字数 654 浏览 1 评论 0原文

我正在用其下方的 Telerik Extensions Tabstrip 替换此“手卷”选项卡条,但我无法理解如何让 Tabstrip 在每个项目的 URL 中包含 target 属性。我怎样才能实现这个目标?

之前:

<ol>
    @foreach (var sheet in Model.Sheets)
    {
        <li>
            <a href="@(Url.Content(Server.MapUrl(sheet.FilePath)) + "?guid=" + Guid.NewGuid())" target="selected-worksheet">@sheet.Name</a></li>
    }
</ol>

之后:

@Html.Telerik().TabStrip().Name("Card").BindTo(Model.Sheets, (item, tabInfo) =>
    {
        item.Text = tabInfo.Name;
        item.Url = Url.Content(Server.MapUrl(tabInfo.FilePath));
    })

I am replacing this 'hand-rolled' tabstrip with the Telerik Extensions Tabstrip below it, but I can't fathom how to get the Tabstrip to include the target attribute in the URL for each item. How can I achieve this?

Before:

<ol>
    @foreach (var sheet in Model.Sheets)
    {
        <li>
            <a href="@(Url.Content(Server.MapUrl(sheet.FilePath)) + "?guid=" + Guid.NewGuid())" target="selected-worksheet">@sheet.Name</a></li>
    }
</ol>

After:

@Html.Telerik().TabStrip().Name("Card").BindTo(Model.Sheets, (item, tabInfo) =>
    {
        item.Text = tabInfo.Name;
        item.Url = Url.Content(Server.MapUrl(tabInfo.FilePath));
    })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

臻嫒无言 2024-12-16 19:50:19

您可以使用 LinkHtmlAttributes 属性来设置额外的 html 属性:

item.LinkHtmlAttributes = new Dictionary<string, object>
{
    { "target", "selected-worksheet" }
};

实际上我从未使用过 Telerik,所以我不太确定是否必须实例化一个新字典或只是添加一个键(如果该属性自动实例化):

item.LinkHtmlAttributes["target"] = "selected-worksheet";

You could use the LinkHtmlAttributes property to set additional html attributes:

item.LinkHtmlAttributes = new Dictionary<string, object>
{
    { "target", "selected-worksheet" }
};

Actually I've never used Telerik, so I am not quite sure if you have to instantiate a new dictionary or simply add a key (in case the property is automatically instantiated):

item.LinkHtmlAttributes["target"] = "selected-worksheet";
凉风有信 2024-12-16 19:50:19

我认为我在使用 Telerik TreeView 时遇到了类似的问题,并通过使用 jQuery 绕道解决了它。

我的问题是,将任何(链接)HtmlAttributes 传递到视图中的 TreeViewItems 不起作用。我尝试向控制器中的 TreeViewItem 添加几个 HtmlAttributes: (例如,我想添加到 元素的一个属性):

 newNodes[i].HtmlAttributes.Add("data-ajax-update","#main");

,但在 return 之后为Ajax 请求的 JsonResult,除了 .Text、.Value、.Enabled、.LoadOnDemand、.Url 之外,视图中的所有内容都为空。

我找到了一个明智的答案,即这些元素没有在 Telerik 论坛中序列化:
http://www.telerik.com/community/forums/aspnet-mvc/treeview/target-blank-on-treeviewitem-url.aspx#1548458

然后我解决了它,添加 特殊

<span class="treeViewItemAddAjaxLocation"></span>

TreeViewItem 的 .Text 的 元素。然后通过视图中的 jQuery 找到这些元素,并将所需的 html 属性添加到 < > 元素。

Telerik TreeView 通过 Ajax 绑定第二和第三阶段元素的方法:

.DataBinding(databinding => databinding.Ajax().Select("_AjaxLoading", "Menu"))

操作“_AjaxLoading”中的控制器片段:

IList<TreeViewItem> newNodes = new List<TreeViewItem>();
foreach (RevisionEntity revision in revisions)
{
    newNodes.Add(new TreeViewItem() 
    {
        Text = "Node Name" + "<span class='treeViewItemAddAjaxLocation'></span>", // name + locator element             
        Value = revision.ID.ToString() + ";RevisionEntity",
        Encoded = false,
        Enabled = true,
        LoadOnDemand = false,
        Url("/Menu/NavigateToRevisionDetails"), // creates an < a > element 
    });
}
return new JsonResult { Data = newNodes };

视图:

<div class="t-bot">
    <a class="t-link t-in" href="/Menu/NavigateToRevisionDetails" data-ajax-update="#main" data-ajax-mode="replace" data-ajax-method="GET" data-ajax="true">Node Name
        <span class="treeViewItemAddAjaxLocation"></span>
    </a>
    <input class="t-input" type="hidden" value="774336a5-c6eb-42cc-905a-4d215c957fa2;RevisionEntity" name="itemValue">
</div>

function TreeView_onLoad(e) {
    var treeView = $(this).data("tTreeView");

    $(".treeViewItemAddAjaxLocation", treeView.element)
        .each(function () {
            var $this = $(this); // put it in jQuery constr
            item = $this.closest(".t-link"); // take the a
            item.attr('data-ajax-update', '#main');
            item.attr('data-ajax-mode', 'replace');
            item.attr('data-ajax-method', 'GET');
            item.attr('data-ajax', 'true');
            $this.remove(); // removes the span element
    });        
}

结果 TreeView 元素:

<div class="t-bot">
    <a class="t-link t-in" href="/Menu/NavigateToRevisionDetails" data-ajax-update="#main" data-ajax-mode="replace" data-ajax-method="GET" data-ajax="true">Node Name</a>
    <input class="t-input" type="hidden" value="774336a5-c6eb-42cc-905a-4d215c957fa2;RevisionEntity" name="itemValue">
</div>

通过 #main html 元素中的 Ajax 加载通过“NavigateToRevisionDetails”操作调用的 PartialView 以及其余部分页面没有刷新!

I think I had a similar problem with the Telerik TreeView and solved it via a detour with jQuery.

My problem was, that it didn't work to pass any (Link)HtmlAttributes, to the TreeViewItems in the View. I tried to add several HtmlAttributes to the TreeViewItem in the Controller: (e.g. one attribute I wanted to add to the < a > element):

 newNodes[i].HtmlAttributes.Add("data-ajax-update","#main");

, but after return as JsonResult of the Ajax-Request, all except .Text, .Value, .Enabled, .LoadOnDemand, .Url were then empty in the View.

I found a sensible answer that these elements are not getting serialized in the Telerik forum:
http://www.telerik.com/community/forums/aspnet-mvc/treeview/target-blank-on-treeviewitem-url.aspx#1548458

I solved it then, with adding a special

<span class="treeViewItemAddAjaxLocation"></span>

element to the .Text of the TreeViewItem. Locating these elements then via jQuery in the View and adding the desired html-attributes to the < a > element.

Telerik TreeView Binding method of the 2nd and 3rd stage elements via Ajax:

.DataBinding(databinding => databinding.Ajax().Select("_AjaxLoading", "Menu"))

Controller snippet in Action "_AjaxLoading":

IList<TreeViewItem> newNodes = new List<TreeViewItem>();
foreach (RevisionEntity revision in revisions)
{
    newNodes.Add(new TreeViewItem() 
    {
        Text = "Node Name" + "<span class='treeViewItemAddAjaxLocation'></span>", // name + locator element             
        Value = revision.ID.ToString() + ";RevisionEntity",
        Encoded = false,
        Enabled = true,
        LoadOnDemand = false,
        Url("/Menu/NavigateToRevisionDetails"), // creates an < a > element 
    });
}
return new JsonResult { Data = newNodes };

View:

<div class="t-bot">
    <a class="t-link t-in" href="/Menu/NavigateToRevisionDetails" data-ajax-update="#main" data-ajax-mode="replace" data-ajax-method="GET" data-ajax="true">Node Name
        <span class="treeViewItemAddAjaxLocation"></span>
    </a>
    <input class="t-input" type="hidden" value="774336a5-c6eb-42cc-905a-4d215c957fa2;RevisionEntity" name="itemValue">
</div>

function TreeView_onLoad(e) {
    var treeView = $(this).data("tTreeView");

    $(".treeViewItemAddAjaxLocation", treeView.element)
        .each(function () {
            var $this = $(this); // put it in jQuery constr
            item = $this.closest(".t-link"); // take the a
            item.attr('data-ajax-update', '#main');
            item.attr('data-ajax-mode', 'replace');
            item.attr('data-ajax-method', 'GET');
            item.attr('data-ajax', 'true');
            $this.remove(); // removes the span element
    });        
}

Result TreeView element:

<div class="t-bot">
    <a class="t-link t-in" href="/Menu/NavigateToRevisionDetails" data-ajax-update="#main" data-ajax-mode="replace" data-ajax-method="GET" data-ajax="true">Node Name</a>
    <input class="t-input" type="hidden" value="774336a5-c6eb-42cc-905a-4d215c957fa2;RevisionEntity" name="itemValue">
</div>

Loads the PartialView invoked through "NavigateToRevisionDetails" Action via Ajax in the #main html-element and the rest of the page doesn't get refreshed!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文