Razor中两列IF ELSE逻辑问题:编辑按钮权限异常
Let's sort out that Edit button issue you're facing. The main problem right now is your conditional logic for enabling/disabling the Edit link is reversed, and you're not properly checking the HasActiveAmendment status when the item is unlocked.
Here's the corrected code with proper logic matching your requirements:
<td class="hidden-xs hidden-sm text-center"> @if (t.IsLocked) { <span class="fa fa-lock fa-lg writer-locked" data-toggle="tooltip" title="This song is locked, pending changes."></span> } else { <span class="fa fa-unlock fa-lg writer-unlocked" data-toggle="tooltip" title="This song is unlocked, @(t.HasActiveAmendment ? "there are pending changes." : "there are no pending changes.")" aria-hidden="true"></span> } </td> <td class="hidden-xs hidden-sm text-center"> @if (t.HasActiveAmendment) { <span class="fa fa-pencil-square-o writers-cross text-center" aria-hidden="true" data-toggle="tooltip" title="This song has been edited, pending changes."></span> } else { <span class="fa fa-minus writers-check text-center" data-toggle="tooltip" title="This song has no pending changes." aria-hidden="true"></span> } </td> <td class="updateTableRow text-center"> <div class="dropdownContainer btn-group text-right"> <button id="@actionWriterId" type="button" class="btn btn-primary br2 btn-xs fs12 dropdown-toggle" data-toggle="dropdown" aria-expanded="false"> Action <span class="caret ml5"></span> </button> <ul class="dropdown-menu dropdown-menu-right" role="menu"> <li> @if (!t.IsLocked && t.HasActiveAmendment) { <!-- Allow edit only when unlocked AND there are pending changes --> <a href="#" id="@editWriterId" data-container="body" data-toggle="tooltip" title="Edit" data-rowhover="editTableRow" class="js_EditWriter">Edit</a> } else { <!-- Disable edit if locked, OR unlocked with no pending changes --> <a class="isDisabled" data-toggle="tooltip" title="@(t.IsLocked ? "This song is locked, you cannot edit it." : "You cannot edit this song until pending writer edits are made.")">Edit</a> } </li> <li> <a href="#" id="@deleteWriterId" data-container="body" data-toggle="tooltip" title="Delete" data-rowhover="deleteTableRow" class="js_DeleteWriter">Delete</a> </li> </ul> </div> </td> </tr> @{ tableRowIndex++; }
Key changes made:
- Fixed the Edit button conditional: Now it only enables the Edit link when the item is not locked AND has active pending changes (
!t.IsLocked && t.HasActiveAmendment), which matches your exact requirement. - Updated tooltip text for disabled state: Differentiated the tooltip message based on why editing is disabled (locked vs no pending changes) to give clearer feedback to users.
- Simplified unlocked state tooltip: Dynamically updated the unlocked icon's tooltip to reflect whether there are pending changes, making the UI more informative.
- Removed redundant commented code: Cleaned up the icon columns by removing duplicate commented-out elements that weren't being used.
This should resolve the issue where the Edit button was disabled when it should be enabled, and ensure the button state correctly aligns with your business rules.
内容的提问来源于stack exchange,提问作者Rob
相关产品推荐
相关产品推荐

