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

如何在ASP.NET C#中根据条件禁用列表项

Solution to Disable Dropdown Items When Account is Locked

Absolutely! You can easily implement this requirement using ASP.NET C# and Razor views. The most secure and straightforward approach is to handle this directly on the server side (since client-side controls can be bypassed), and we can add extra safeguards to ensure the functionality works as expected.

Since you're already checking t.IsLocked == 1 to show the lock icon, you can extend this logic to disable the target dropdown items (I’ll assume you want to disable Edit and Delete based on your description—adjust the items as needed).

Modify your partial view code for the dropdown items like this:

<ul class="dropdown-menu dropdown-menu-right" role="menu">
    <li>
        <a href="@Url.Action("Recordings", "ArtistAccount", new { accountcode = fullAccountCode, songcode = t.SongId })" id="@recordingWorkId" data-container="body" data-rowhover="editTableRow" class="js_Recordings">Recordings</a>
    </li>
    <li>
        @if (t.IsLocked == 1)
        {
            <!-- Show a non-clickable disabled element when account is locked -->
            <span class="js_EditSong disabled text-muted" data-container="body" data-rowhover="editTableRow">Edit</span>
        }
        else
        {
            <!-- Normal clickable link when account is unlocked -->
            <a href='@Url.Action("EditSong", "ArtistAccount", new { songcode = t.SongId, accountcode = fullAccountCode, page = Model.PagingInfo.Page, take = Model.PagingInfo.Take, sortBy = Model.PagingInfo.SortPropertyName, sortAsc = Model.PagingInfo.SortAscending })' id="@editWorkId" data-container="body" data-rowhover="editTableRow" class="js_EditSong">Edit</a>
        }
    </li>
    <li>
        @if (t.IsLocked == 1)
        {
            <span class="js_DeleteSong disabled text-muted" data-container="body" data-toggle="tooltip" title="Delete (Account Locked)">Delete</span>
        }
        else
        {
            <a href="#" id="@deleteWorkId" data-container="body" data-toggle="tooltip" title="Delete" data-rowhover="deleteTableRow" class="js_DeleteSong">Delete</a>
        }
    </li>
    <li>
        <a href="@Url.Action("TuneCodes", "ArtistAccount", new { accountCode = fullAccountCode, workCode = t.SongId, })" id="@tuneCodeId" data-container="body" data-toggle="tooltip" title="Tune Codes" data-rowhover="editTableRow">Tune Codes</a>
    </li>
</ul>

Add this CSS to your stylesheet to make disabled items look unclickable:

.disabled {
    pointer-events: none;
    cursor: default;
    opacity: 0.6;
}

2. Server-Side Action Validation (Critical Safeguard)

Even if you disable the items in the view, always validate the account state in your controller actions to prevent unauthorized access (since users can bypass client-side restrictions):

public ActionResult EditSong(string songcode, string accountcode)
{
    // Fetch the actual account from your database (replace with your data access logic)
    var account = _dbContext.Accounts.FirstOrDefault(a => a.AccountCode == accountcode);
    
    // Check if account is locked (adjust the condition to include your `state` column if needed)
    if (account != null && account.IsLocked == 1 && account.State == 1)
    {
        TempData["ErrorMessage"] = "Cannot edit song: This account is locked.";
        return RedirectToAction("YourListAction"); // Redirect back to the list view
    }

    // Proceed with normal edit logic
    // ...
}

public ActionResult DeleteSong(string songcode, string accountcode)
{
    var account = _dbContext.Accounts.FirstOrDefault(a => a.AccountCode == accountcode);
    
    if (account != null && account.IsLocked == 1 && account.State == 1)
    {
        TempData["ErrorMessage"] = "Cannot delete song: This account is locked.";
        return RedirectToAction("YourListAction");
    }

    // Proceed with normal delete logic
    // ...
}

3. Client-Side JS (Optional, for Enhanced UX)

If you want to add extra client-side feedback (like preventing accidental clicks even if the DOM is modified), you can add this jQuery snippet:

$(document).ready(function() {
    // Find all rows with locked accounts and disable target dropdown items
    $('span.locked').closest('tr').find('.js_EditSong, .js_DeleteSong').each(function() {
        $(this).addClass('disabled').on('click', function(e) {
            e.preventDefault();
            alert("This action is unavailable: Account is locked."); // Optional feedback
            return false;
        });
    });
});

This is a supplementary measure—always rely on server-side validation as the primary safeguard.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:19:54