You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# Razor语法求助:Kendo Tab可选Content设置问题

Hey there! Since you’re coming from Node.js/PHP/Ruby and just dipping your toes into C# Razor + Kendo UI, let’s break down how to fix those two issues you’re facing:


1. Fixing the @div issue in JavaScript

The problem here is that Razor interprets the @ symbol as a server-side code marker. When you write @div directly in JavaScript, Razor tries to resolve it as a server-side variable (which probably doesn’t exist, or isn’t what you intend). Here’s how to handle it properly:

Scenario A: You want to reference a DOM element with an ID stored in a server variable

If div is a server-side variable holding the ID of a DOM element, you need to let Razor output the ID as a string for JavaScript to use:

@{
    string divId = "my-custom-div"; // Server-side variable
}

<script>
    // Razor will replace @divId with the actual string value
    var targetDiv = document.getElementById('@divId');
    // Now you can work with targetDiv in JS
</script>

Scenario B: You want to pass server-side string content to JavaScript

If div is a server-side string with HTML or text content, use Json.Encode + Html.Raw to safely pass it to JS:

@{
    string divContent = "<p>Hello from server!</p>";
}

<script>
    var serverContent = @Html.Raw(Json.Encode(divContent));
    // serverContent will be a properly escaped JS string you can use
    document.getElementById('some-div').innerHTML = serverContent;
</script>

2. Adding conditional content in Kendo Tab’s .Content() method

Kendo’s Fluent API works with Razor’s server-side logic, so you have a couple clean ways to add optional content:

Option 1: Use Razor’s @<text> syntax directly in .Content()

This is great for simple conditions, since it lets you write HTML mixed with server-side logic without extra wrapper tags:

@(Html.Kendo().TabStrip()
    .Name("myTabStrip")
    .Items(tabstrip => {
        tabstrip.Add()
            .Text("My Tab")
            .Content(@<text>
                <p>Default tab content here</p>
                <!-- Conditional server-side content -->
                @if (Model.ShouldShowOptionalSection) {
                    <div class="optional-section">
                        <h4>Optional Content</h4>
                        <p>This only shows if the condition is true!</p>
                    </div>
                }
            </text>);
    })
)

Option 2: Build content dynamically with a StringBuilder

For more complex logic (like multiple conditions or dynamic content generation), pre-build your content server-side first:

@{
    var tabContent = new StringBuilder();
    tabContent.Append("<p>Base content for the tab</p>");

    // Add optional content based on your condition
    if (User.IsInRole("Admin")) {
        tabContent.Append(@"
            <div class='admin-controls'>
                <button>Admin Only Action</button>
            </div>
        ");
    }
}

@(Html.Kendo().TabStrip()
    .Name("adminTabStrip")
    .Items(tabstrip => {
        tabstrip.Add()
            .Text("Admin Dashboard")
            .Content(tabContent.ToString());
    })
)

内容的提问来源于stack exchange,提问作者Mr Heelis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:42:37